[求助-高手]百思不得其解问题-线程

mouer 2010-03-11 06:46:59
public class Main extends Thread {
@Override
public void run() {
while(!this.isInterrupted()){
System.out.println("the thread is run ...");
try {
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException e) {
// ignore
}
}

}

public void goOn(){
Main.interrupted();
if(!this.isInterrupted()){
run();
}
}

public static void main(String[] args) throws InterruptedException{
Main td = new Main();
System.out.println("``````````````````````````````````````");
td.start();
TimeUnit.SECONDS.sleep(2L);
System.out.println("use interrupt()...");
td.interrupt(); // ...不好用?
TimeUnit.SECONDS.sleep(4L);
System.out.println("use goOn()...");
td.goOn();
}
}
有时候结果是:(预期)
`````````````````````````````````````
the thread is run ...
the thread is run ...
use interrupt()...
use goOn()...
the thread is run ...
the thread is run ...
the thread is run ...
有时候(非预期):
``````````````````````````````````````
the thread is run ...
the thread is run ...
use interrupt()...
the thread is run ...
the thread is run ...
the thread is run ...
the thread is run ...
use goOn()...
the thread is run ...
the thread is run ...
the thread is run ...
the thread is run ...
the thread is run ...

jdk1.6_18
...全文
126 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mouer 2010-03-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 jimyx123 的回复:]
还有可能中断失败了
* <p> If this thread is blocked in an invocation of the {@link
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.
如果要中断的线程正在等待,中断标志会被清除并抛出异常,看一你的sleep是不是抛异常了 中断不成功
[/Quote]
正解...发现问题了,问题出现在可能有时候TimeUnit.SECONDS.sleep(1L)和interrupt()同时运行了.嗯
3Q
jimyx123 2010-03-12
  • 打赏
  • 举报
回复
还有可能中断失败了
* <p> If this thread is blocked in an invocation of the {@link
* Object#wait() wait()}, {@link Object#wait(long) wait(long)}, or {@link
* Object#wait(long, int) wait(long, int)} methods of the {@link Object}
* class, or of the {@link #join()}, {@link #join(long)}, {@link
* #join(long, int)}, {@link #sleep(long)}, or {@link #sleep(long, int)},
* methods of this class, then its interrupt status will be cleared and it
* will receive an {@link InterruptedException}.
如果要中断的线程正在等待,中断标志会被清除并抛出异常,看一你的sleep是不是抛异常了 中断不成功
mouer 2010-03-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼]
这个要看JVM的调度算法了。。。
[/Quote]
[Quote=引用 6 楼]
jvm调度算法问题
[/Quote]

.....我去找找JVM资料
lttto 2010-03-12
  • 打赏
  • 举报
回复
jvm调度算法问题
mouer 2010-03-12
  • 打赏
  • 举报
回复
我就是感觉 td.interrupt(); // ...不好用?
mouer 2010-03-12
  • 打赏
  • 举报
回复
2个线程没错,但输出
the thread is run ...
the thread is run ...
use interrupt()...
证明:
1. run() 已经执行了.
2.运行interrupt()后,线程应该处于中断状态,休息四秒..但问题是
为预期情况
the thread is run ...
the thread is run ...
use interrupt()...
the thread is run ...
the thread is run ...
the thread is run ...
没有出现等待...
jimyx123 2010-03-12
  • 打赏
  • 举报
回复
如果想都得到 (预期)结果,可以在while里加锁,中断前先获取锁
不然结果没法预见.发送中断消息后要先获取中断锁 这时候while可能已被调度
Dazzlingwinter 2010-03-12
  • 打赏
  • 举报
回复
这个要看JVM的调度算法了。。。
梦_枫 2010-03-12
  • 打赏
  • 举报
回复
System.out.println("use goOn()...");
System.out.println("use interrupt()...");
这二句话是属于主线程中的public void static mian()......这个方法是另外一个线程也就是程序的主线程。。。

你的程序一共有二个线程:主线程和你自己写的Main类线程..

线程执行的顺序本来就是由jvm来调度的,没有一个定死的顺序,
所以结果出来的并不会一样,,,
Warrior_hsn 2010-03-12
  • 打赏
  • 举报
回复
这个interrupt(); 有问题。

public class Main extends Thread {
volatile boolean flag = false;

@Override
public void run() {
while (!flag) {
System.out.println("the thread is run ...");
try {
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException e) {
// ignore
}
}

}

public void goOn() {
if (flag) {
flag = false;
run();
}
}

public static void main(String[] args) throws InterruptedException {
Main td = new Main();
System.out.println("``````````````````````````````````````");
td.start();
TimeUnit.SECONDS.sleep(2L);
td.flag = true;
System.out.println("use interrupt()...");
TimeUnit.SECONDS.sleep(4L);
System.out.println("use goOn()...");
td.goOn();
}
}
qq369759459 2010-03-12
  • 打赏
  • 举报
回复
线程问题 本人PASS 完全不理解

62,614

社区成员

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

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