java暂停其他线程_如何从另一个线程暂停和恢复Java中的线程

我正在用Java Swing编写应用程序。我需要的是一个可以使用图形界面中的按钮停止“阐述”线程的过程。

这里有一个简单的项目专注于我的需求

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")

//

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();

}//

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

*/

//

/*

* 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);

}

//

/*

* 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();

}

}

}

问题很简单:在实际的应用程序中,用户设置一些选项,然后启动线程,对选定的数据进行详细说明。

我想提供一个“暂停”按钮,以便用户可以暂时停止详细说明并进行必要的检查,然后可以恢复操作。

我编码的方式是停止的图形线程,而不是“精化”线程。

如果运行示例代码并按“开始”,则文本区域开始计数。我需要的最终结果是,当我按下“暂停”按钮时,线程进入“睡眠”状态,计数停止;当我按下“恢复”按钮时,线程“唤醒”,文本区域中的计数开始再数一次。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值