如何从另一个线程中暂停和恢复Java线程

我正在用Java Swing编写一个应用程序。 我需要的是一个程序,我可以使用graphics界面中的button来停止“精化”线程。

这里有一个简单的项目,侧重于我所需要的

import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JTextArea; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Nikola */ public class Main extends javax.swing.JFrame { private MyThread THREAD; public Main() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jButton1 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Pause Thread"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jButton2.setText("Resume Thread"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Start Thread"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jButton3) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 63, Short.MAX_VALUE) .addComponent(jButton2) .addGap(18, 18, 18) .addComponent(jButton1)) .addComponent(jScrollPane1)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jButton1) .addComponent(jButton2) .addComponent(jButton3)) .addContainerGap()) ); pack(); }// </editor-fold> private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { THREAD = new MyThread(jTextArea1); THREAD.start(); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { try { THREAD.pauseThread(); } catch (InterruptedException ex) { ex.printStackTrace(); } } private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { THREAD.resumeThread(); } public static void main(String args[]) { /* * Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* * If Nimbus (introduced in Java SE 6) is not available, stay with the * default look and feel. For details see * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* * Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Main().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; // End of variables declaration } class MyThread extends Thread { JTextArea area; private final Object lock = new Object(); public MyThread(JTextArea area) { super(); this.area = area; } @Override public void run() { for(int i=0 ; ; i++) area.setText(i+""); } public void pauseThread() throws InterruptedException { synchronized(lock) { lock.wait(); } } public void resumeThread() { synchronized(lock) { lock.notify(); } } } 

问题很简单:在真正的应用程序中,用户设置了一些选项,然后启动线程来完成所选数据的细化。

我想提供一个“暂停”button,这样用户可以暂时停止说明并做一些必要的检查,之后可以恢复操作。

在我编码的方式是停止的graphics线程,而不是“阐述”之一。

如果你运行我的示例代码,然后按“开始”textarea开始计数。 最后的结果,我需要的是,当我按下“暂停”button的线程去“睡眠”,计数停止,当我按下“恢复”button线程“醒来”,在文本区域的计数开始再计算一次。

你不能以你想要的方式明确地暂停一个线程。

你需要做的,是通过设置某种标志来表明另一个线程应该停止。 有问题的线程必须有逻辑来检查这个标志,并在发生这种情况时暂停工作。

所以在这个特殊的情况下,也许改变MyThread如下:

 class MyThread extends Thread { private volatile boolean running = true; // Run unless told to pause ... @Override public void run() { for(int i=0 ; ; i++) { // Only keep painting while "running" is true // This is a crude implementation of pausing the thread while (!running) yield; area.setText(i+""); } public void pauseThread() throws InterruptedException { running = false; } public void resumeThread() { running = true; } } 

这是一个粗略的例子,为了简洁,使用了一种自旋锁而不是适当的基于监视器的睡眠。 希望尽pipe它传达了如何使用标志来控制线程暂停的想法。

请注意,如果在块中执行一些长时间运行的步骤,而不是只调用setText ,那么最好在每个步骤之间检查Thread.currentThread().interrupted()并退出循环如果itnerrupt标志被设置。 这是广泛的内置阻塞方法(如I / O),以便他们可以被其他线程中断 – 因为running标志只检查一个循环,如果每个循环循环需要20分钟。

试试像这样:

 class MyThread extends Thread { JTextArea area; private final Object GUI_INITIALIZATION_MONITOR = new Object(); private boolean pauseThreadFlag = false; public MyThread(JTextArea area) { super(); this.area = area; } @Override public void run() { for(int i=0 ; ; i++) { checkForPaused(); area.setText(i+""); } } private void checkForPaused() { synchronized (GUI_INITIALIZATION_MONITOR) { while (pauseThreadFlag) { try { GUI_INITIALIZATION_MONITOR.wait(); } catch (Exception e) {} } } } public void pauseThread() throws InterruptedException { pauseThreadFlag = true; } public void resumeThread() { synchronized(GUI_INITIALIZATION_MONITOR) { pauseThreadFlag = false; GUI_INITIALIZATION_MONITOR.notify(); } } } 

像你一样使用显示器是一个很好的理念。 但是你不能强迫外面的等待。 你必须告诉线程等待,直到你再次通知他(通过显示器)。 在这种情况下,你只需要这个简单的方法checkForPaused() ,你将不得不把它放在战略位置,所以没有很长的延迟,直到线程暂停。

你也可以扩展这个函数,让你可以询问线程是否用checkForPaused()public boolean isPausing()设置的标志暂停。