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
@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
System.out.println("use interrupt()...");
这二句话是属于主线程中的public void static mian()......这个方法是另外一个线程也就是程序的主线程你的程序一共有二个线程:主线程和你自己写的Main类线程..线程执行的顺序本来就是由jvm来调度的,没有一个定死的顺序,
所以结果出来的并不会一样,,,
不然结果没法预见.发送中断消息后要先获取中断锁 这时候while可能已被调度
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 ...
没有出现等待...
* <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是不是抛异常了 中断不成功
3Q
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();
}
}