线程中途停止的问题

hearrt 2009-01-08 11:23:21

Thread t = new Thread(){
public void run(){
Thread th1 = new Thread();
Thread th2 = new Thread();
Thread th3 = new Thread();
Thread th4 = new Thread();

//这四个线程分别运行5秒

try {
th1.start();
th1.join();

th2.start();
th2.join();

th3.start();
th3.join();

th4.start();
th4.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();

问一下:总线程t启动后,当运行th2过程中 怎样才可以把总线程t立即停止 我调用t.interrupt() 或者t.yield()语句后,它还是会继续执行th3线程和th4线程。
但是当th4线程运行时候 我去调用t.interrupt()语句 t线程就会停止运行。
...全文
93 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ydplus 2009-01-08
  • 打赏
  • 举报
回复
可能是你th2实现的有问题,你可以把完整的代码贴出来看看。

th2.start();
th2.join();
当线程t block时,th2调t.interrupte(), 线程t会抛InterruptedException,
th3,th4应该没有机会执行.

hearrt 2009-01-08
  • 打赏
  • 举报
回复

Thread t = new Thread(){
public void run(){
Thread th1 = new Thread();
Thread th2 = new Thread();
Thread th3 = new Thread();
Thread th4 = new Thread();

//这四个线程分别运行5秒

try {
if(!stop){//设置一个boolean变量 现在也不去调用interrupt函数了 这样可行吗?
th1.start();
th1.join();
}

if(!stop){
th2.start();
th2.join();
}

if(!stop){
th3.start();
th3.join();
}

if(!stop){
th4.start();
th4.join();
}

} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();



设置一个boolean变量 在调用interrupt函数的地方改变boolean变量的值 这样可行吗? 效果上差不多了
hearrt 2009-01-08
  • 打赏
  • 举报
回复
之前遇到的都是一个线程的 现在线程里套线程 路过的帮忙给看看 困恼了一阵子了

13,100

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧