50,528
社区成员
发帖
与我相关
我的任务
分享
public class A extends Thread {
@Override
public void run() {
process();
}
private void process() {
/*......*/
}
}
public class B {
A a;
private void function() {
//......
JButton button_start = new JButton("开始");
button_start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
a = new A();
a.start();
}
});
JButton button_pause = new JButton("暂停");
button_pause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 在这里让线程a暂停
}
});
JButton button_resume = new JButton("恢复");
button_resume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 在这里让线程a继续
}
});
//......
}
}
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
class ThreadClass extends JTextField implements Runnable {
//--- flag 控制线程状态 ---
private boolean flag = true;
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
//--- 通过累计计数模拟线程运行---
int times = 0;
//--- 设置文本框主要是显示线程状态 ---
public ThreadClass(String s) {
super(s);
setBackground(Color.ORANGE);//---橘黄
setEditable(false);
setHorizontalAlignment(JTextField.CENTER);
}
@Override
public void run() {
process();
}
private synchronized void process() {
while (true) {//无限循环
if (flag == true) {
this.setText("This is the " + (times++)
+ "th times running \n");
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
ie.printStackTrace();
}
notify();//--- 唤醒另一线程 --- ,本代码其实没有其他等待线程,这个可不用 ---
} else {
this.setText(" When times = " + times + "\n The thread is paused!");
try {
wait();//--- 进入等待
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
}
}
public class ThreadControll extends JFrame {
ThreadClass a = new ThreadClass("请按开始按钮!");
Thread startThread = null;
public ThreadControll(String s) {
super(s);
final Container c = getContentPane();
c.setLayout(new BorderLayout());
JPanel jp = new JPanel(new FlowLayout());
JButton button_start = new JButton("开始");
JButton button_pause = new JButton("暂停");
JButton button_resume = new JButton("恢复");
jp.add(button_start);
jp.add(button_pause);
jp.add(button_resume);
//--- 开始按钮只执行一次 ---
button_start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if (null == startThread) {
startThread = new Thread(a);
startThread.start();
}
}
});
//--- 暂停按钮 ,按下后如果没有按继续,就不改变标志 ---
button_pause.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (a.isFlag() != false) {
a.setFlag(false);
}
}
});
//--- 恢复按钮,这个最关键,主要是用notify()让线程继续执行,---
button_resume.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (a.isFlag() == false) {
a.setFlag(true);
synchronized (a) {//--- 如果不放在同步快里,无法实现唤醒 ---
a.notify();//--- 这个代码执行完后,a对象锁就释放了 所以process() 方法里德 notify()可不用---
}
}
}
});
c.add(jp,BorderLayout.NORTH);
c.add(a, BorderLayout.CENTER);
}
//--- 入口 ---
public static void main(String[] args) {
ThreadControll tc = new ThreadControll("test");
tc.setSize(400, 200);
tc.setVisible(true);
tc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
@Deprecated
public final void suspend()
Deprecated. This method has been deprecated, as it is inherently deadlock-prone. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, deadlock results. Such deadlocks typically manifest themselves as "frozen" processes. For more information, see Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?.
Suspends this thread.
First, the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException (in the current thread).
If the thread is alive, it is suspended and makes no further progress unless and until it is resumed.
Throws:
SecurityException - if the current thread cannot modify this thread.
See Also:
checkAccess()
我觉得你的情况可以使用,因为从你的代码来推测,你的没有资源抢占,应该不会造成死锁问题
个人简单想法,仅供参考