日期:2014-05-20  浏览次数:20831 次

如何让一个线程中的函数暂停执行?
本帖最后由 zhengkai2001 于 2014-04-04 21:27:04 编辑

A类继承Thread,B类中包含A类的实例a,同时通过两个按钮来控制a中的线程的暂停执行与恢复执行,下面是代码示意:


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继续
}
});
//......
}
}



在不改变process()函数体的情况下,这个暂停和继续能够实现吗?
------解决方案--------------------
我没在我机器上实验过哈,提出小小的想法

A是线程的子类
直接调用线程的suspend()方法行不行?在调用前线判断该线程状态,下一个问题同样如此,调用resume方法恢复

具体请参考API文档

关于这些方法在1.7中已经不建议使用
@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()


我觉得你的情况可以使用,因为从你的代码来推测,你的没有资源抢占,应该不会造成死锁问题

个人简单想法,仅供参考
------解决方案--------------------
利用wait() notify() 写了一个。

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;