如何打断一个正在顺序执行的线程 我面板上有两个按钮,一个是“start",一个是 "cancel",我想点start让线程顺序执行,点cancel结束正在顺序执行的线程,那么如何实现我想不出来,以前都是循环执行的线程还好搞些,顺序执行的我实在不知道怎么中断了,我本来有个方案是使用yield()好像没什么效果 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 void interrupt() 中断线程。还有一个void stop()方法 不过已经过时 不推荐使用(极易产生死锁) interrupt() 最合适吧,sleep()其次 不知道我理解的有没有误啊。加标志位进行判断下。下面,是我以前遇到的一个程序。也是两个Button,情况类似。import java.awt.*;import java.awt.event.*;import java.applet.*;public class FrameGO extends Applet implements Runnable{ private int count = 0; private Button onOff = new Button("Cancel"), start = new Button("Start"); private TextField t = new TextField(10); boolean runFlag = false; static Thread th; public void init() { add(t); start.addActionListener(new StartL()); add(start); onOff.addActionListener(new OnOffL()); add(onOff); } public void run() { count = 0; while (true) { try { Thread.sleep(100); } catch (InterruptedException e) { } if (runFlag) count++; t.setText(Integer.toString(count)); } } class StartL implements ActionListener { public void actionPerformed(ActionEvent e) { runFlag = true; } } class OnOffL implements ActionListener { public void actionPerformed(ActionEvent e) { runFlag = false; } } public static void main(String[] args) { FrameGO applet = new FrameGO(); Frame aFrame = new Frame("Counter1"); th = new Thread(applet); th.start(); aFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); aFrame.add(applet, BorderLayout.CENTER); aFrame.setSize(300, 200); applet.init(); applet.start(); aFrame.setVisible(true); }} public class MyClass implements Runnable {private volatile boolean stopped = true;public void run() { while (stopped == false) { // 关键之处 // 悄悄的干些事. }}public void start() { stopped = false; new Thread(this).start();}public void stop() { stopped = true;}}-----------------------------------------------华丽的分划线.-----------------MyClass myClass = new MyClass();startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myClass.start(); }});startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myClass.stop(); }}); 用:void interrupt()来中断线程,吧。 interrupt怎么用?我用怎么不行? 如果线程因拿不到锁定而进入等待状况,是没有办法将其打断的你看看这段代码对你有没有帮助吧public class ThreadTest6 implements Runnable {private final static String BLANK = " ";public static void main(String[] args) {ThreadTest6 test = new ThreadTest6();Thread th = new Thread(test, "ThreadTest6");System.out.println(Thread.currentThread().getName() + "准备运行");th.setDaemon(true);th.start();System.out.println(Thread.currentThread().getName() + "正在运行中");for (int i = 0; i < 100; i++) { System.out.println("尝试打断" + th.getName() + "休眠"); th.interrupt(); try { Thread.sleep(100l); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + "运行结束"); } public void run() { while (true) { System.out.println(Thread.currentThread().getName() + "运行中"); for (int j = 0; j < 10000; j++) { System.out.println(Thread.currentThread().getName() + "运行中" + BLANK + j); } try { Thread.sleep(1000l); } catch (InterruptedException e) { System.out.println("休眠被打断"); } } } } 我本来想用把标志位设为FALSE,同时调用yield()的,但不行,哎 输出123454321 有个问题,有点搞笑 类之间最常见的关系,会的帮写以下好么?谢谢啊! 哪位帮传一个JAVA与SQL2000的驱动程序,小弟笨了点没有找到 Java -version 我的版本是1.3, 怎样安装1.5? 有下载地址吗? 怎样把一个字符串专成file文件类型 求帮忙写个算法 getActionCommand()是什么东东? 如何在TOMCAT3.0里配置一个数据源和连接池? ,一定给分!! 利用jb组件pop,如何获取邮件头部信息 JAVA的3D程序 swing焦点问题,谢谢
中断线程。
还有一个void stop()方法 不过已经过时 不推荐使用(极易产生死锁)
sleep()其次
加标志位进行判断下。
下面,是我以前遇到的一个程序。
也是两个Button,情况类似。import java.awt.*;
import java.awt.event.*;
import java.applet.*;public class FrameGO extends Applet implements Runnable
{
private int count = 0; private Button onOff = new Button("Cancel"), start = new Button("Start"); private TextField t = new TextField(10); boolean runFlag = false;
static Thread th;
public void init()
{
add(t);
start.addActionListener(new StartL());
add(start);
onOff.addActionListener(new OnOffL());
add(onOff);
} public void run()
{
count = 0;
while (true)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
}
if (runFlag)
count++;
t.setText(Integer.toString(count));
}
} class StartL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
runFlag = true;
}
} class OnOffL implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
runFlag = false;
}
} public static void main(String[] args)
{
FrameGO applet = new FrameGO();
Frame aFrame = new Frame("Counter1");
th = new Thread(applet);
th.start();
aFrame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(300, 200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
public class MyClass implements Runnable {
private volatile boolean stopped = true;public void run() {
while (stopped == false) { // 关键之处
// 悄悄的干些事.
}
}public void start() {
stopped = false;
new Thread(this).start();
}public void stop() {
stopped = true;
}
}
-----------------------------------------------华丽的分划线.-----------------
MyClass myClass = new MyClass();startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myClass.start();
}
});startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myClass.stop();
}
});
void interrupt()
来中断线程,
吧。
你看看这段代码对你有没有帮助吧public class ThreadTest6 implements Runnable {
private final static String BLANK = " ";public static void main(String[] args) {
ThreadTest6 test = new ThreadTest6();
Thread th = new Thread(test, "ThreadTest6");
System.out.println(Thread.currentThread().getName() + "准备运行");
th.setDaemon(true);
th.start();
System.out.println(Thread.currentThread().getName() + "正在运行中");
for (int i = 0; i < 100; i++) { System.out.println("尝试打断" + th.getName() + "休眠"); th.interrupt(); try { Thread.sleep(100l); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + "运行结束"); } public void run() { while (true) { System.out.println(Thread.currentThread().getName() + "运行中"); for (int j = 0; j < 10000; j++) { System.out.println(Thread.currentThread().getName() + "运行中" + BLANK + j); } try { Thread.sleep(1000l); } catch (InterruptedException e) { System.out.println("休眠被打断"); } } } }