锁定老帖子 主题:多线程 interrupt() 中断
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-10-19
最后修改:2011-10-19
线程中断我也一直没搞清楚,借楼主的贴问问。 public class ThreadInterrupt {
/**
* @param args
*/
public static void main(String[] args) {
A a = new A();
a.start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
a.interrupt();// 中断
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("[main] a.isInterrupted()---:" + a.isInterrupted()); //这里应该打印什么?
}
}
class A extends Thread {
public A(){
}
public void run(){
while(!isInterrupted()){
System.out.println("[A] 在中断前---:" + isInterrupted());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("[A] 在中断异常1---:" + isInterrupted());
e.printStackTrace();
interrupt(); // 在异常中再次中断
System.out.println("[A] 在中断异常2---:" + isInterrupted());
}
}
}
}
|
|
返回顶楼 | |
发表时间:2011-10-19
hardPass 写道
线程中断我也一直没搞清楚,借楼主的贴问问。 public class ThreadInterrupt {
/**
* @param args
*/
public static void main(String[] args) {
A a = new A();
a.start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
a.interrupt();// 中断
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("[main] a.isInterrupted()---:" + a.isInterrupted()); //这里应该打印什么?
}
}
class A extends Thread {
public A(){
}
public void run(){
while(!isInterrupted()){
System.out.println("[A] 在中断前---:" + isInterrupted());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("[A] 在中断异常1---:" + isInterrupted());
e.printStackTrace();
interrupt(); // 在异常中再次中断
System.out.println("[A] 在中断异常2---:" + isInterrupted());
}
}
}
}
确实有这个问题,希望高人 解答 |
|
返回顶楼 | |
发表时间:2011-10-19
try catch 应该在 while的外面
|
|
返回顶楼 | |
发表时间:2011-10-19
hardPass 写道
线程中断我也一直没搞清楚,借楼主的贴问问。 public class ThreadInterrupt {
/**
* @param args
*/
public static void main(String[] args) {
A a = new A();
a.start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
a.interrupt();// 中断
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("[main] a.isInterrupted()---:" + a.isInterrupted()); //这里应该打印什么?
}
}
class A extends Thread {
public A(){
}
public void run(){
while(!isInterrupted()){
System.out.println("[A] 在中断前---:" + isInterrupted());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("[A] 在中断异常1---:" + isInterrupted());
e.printStackTrace();
interrupt(); // 在异常中再次中断
System.out.println("[A] 在中断异常2---:" + isInterrupted());
}
}
}
}
|
|
返回顶楼 | |
发表时间:2011-10-19
When a blocking method detects interruption and throws InterruptedException, it clears the interrupted status
http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html |
|
返回顶楼 | |
发表时间:2011-10-19
public class Main { public static boolean flag = false; public static void main(String[] args) { A a = new A(); a.start(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } a.interrupt();// 中断 try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("[main]1 a.isInterrupted()---:" + a.isInterrupted()); flag = true; try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("[main]2 a.isInterrupted()---:" + a.isInterrupted()); } } class A extends Thread { public A(){ } public void run(){ while(!isInterrupted()){ System.out.println("[A] 在中断前---:" + isInterrupted()); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("[A] 在中断异常1---:" + isInterrupted()); e.printStackTrace(); interrupt(); // 在异常中再次中断 System.out.println("[A] 在中断异常2---:" + isInterrupted()); } } System.out.println("[A] 等待主线程---:" + isInterrupted()); while(!Main.flag); System.out.println("[A] 结束退出---:" + isInterrupted()); } } 输出: [A] 在中断前---:false
很明显 线程如果执行完毕了也算false |
|
返回顶楼 | |
发表时间:2011-10-20
seahb 写道
public class Main { public static boolean flag = false; public static void main(String[] args) { A a = new A(); a.start(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } a.interrupt();// 中断 try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("[main]1 a.isInterrupted()---:" + a.isInterrupted()); flag = true; try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("[main]2 a.isInterrupted()---:" + a.isInterrupted()); } } class A extends Thread { public A(){ } public void run(){ while(!isInterrupted()){ System.out.println("[A] 在中断前---:" + isInterrupted()); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("[A] 在中断异常1---:" + isInterrupted()); e.printStackTrace(); interrupt(); // 在异常中再次中断 System.out.println("[A] 在中断异常2---:" + isInterrupted()); } } System.out.println("[A] 等待主线程---:" + isInterrupted()); while(!Main.flag); System.out.println("[A] 结束退出---:" + isInterrupted()); } } 输出: [A] 在中断前---:false
很明显 线程如果执行完毕了也算false 任务执行完了 中断状态 清除了 |
|
返回顶楼 | |
发表时间:2011-10-20
很有道理,你让A sleep(10),就OK
|
|
返回顶楼 | |
发表时间:2011-10-20
firefoxmmxx 写道 我认为 执行interrupt 会抛出一个中断异常。而且中断线程就是靠这个异常机制执行的。如果把这段代码TRY CATCH了。中断当然就停止了。
我也这么认为。。 |
|
返回顶楼 | |
发表时间:2011-10-20
对于没有处于阻塞状态(wait/sleep/join等)的线程,java不能真正的终止线程,需要你自己在代码里判断Thread.interrupted(),然后抛出自己的异常,相当于线程终止。
|
|
返回顶楼 | |