论坛首页 Java企业应用论坛

多线程 interrupt() 中断

浏览 11459 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-10-19   最后修改:2011-10-19

线程中断我也一直没搞清楚,借楼主的贴问问。

在A线程的异常中再次中断,程序是停止了,但是打印的结果却不是预料中的,这是为什么?

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());
			}
		}
		
	}

}





程序执行打印:
[A] 在中断前---:false
[A] 在中断异常1---:false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.wl.test.concurrent.thread.A.run(ThreadInterrupt.java:38)
[A] 在中断异常2---:true
[main] a.isInterrupted()---:false  // 这里为什么打印出false?



 

1 请登录后投票
   发表时间:2011-10-19  
hardPass 写道

线程中断我也一直没搞清楚,借楼主的贴问问。

在A线程的异常中再次中断,程序是停止了,但是打印的结果却不是预料中的,这是为什么?

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());
			}
		}
		
	}

}





程序执行打印:
[A] 在中断前---:false
[A] 在中断异常1---:false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.wl.test.concurrent.thread.A.run(ThreadInterrupt.java:38)
[A] 在中断异常2---:true
[main] a.isInterrupted()---:false  // 这里为什么打印出false?


 

确实有这个问题,希望高人 解答

0 请登录后投票
   发表时间:2011-10-19  
try catch 应该在 while的外面
0 请登录后投票
   发表时间:2011-10-19  
hardPass 写道

线程中断我也一直没搞清楚,借楼主的贴问问。

在A线程的异常中再次中断,程序是停止了,但是打印的结果却不是预料中的,这是为什么?

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());
			}
		}
		
	}

}





程序执行打印:
[A] 在中断前---:false
[A] 在中断异常1---:false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.wl.test.concurrent.thread.A.run(ThreadInterrupt.java:38)
[A] 在中断异常2---:true
[main] a.isInterrupted()---:false  // 这里为什么打印出false?



 


0 请登录后投票
   发表时间: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
0 请登录后投票
   发表时间: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
[A] 在中断异常1---:false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at A.run(Hello.java:45)
[A] 在中断异常2---:true
[A] 等待主线程---:true
[main]1 a.isInterrupted()---:true
[A] 结束退出---:true
[main]2 a.isInterrupted()---:false

 

很明显 线程如果执行完毕了也算false

1 请登录后投票
   发表时间: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
[A] 在中断异常1---:false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at A.run(Hello.java:45)
[A] 在中断异常2---:true
[A] 等待主线程---:true
[main]1 a.isInterrupted()---:true
[A] 结束退出---:true
[main]2 a.isInterrupted()---:false

 

很明显 线程如果执行完毕了也算false

   任务执行完了 中断状态 清除了

1 请登录后投票
   发表时间:2011-10-20  
很有道理,你让A sleep(10),就OK
0 请登录后投票
   发表时间:2011-10-20  
firefoxmmxx 写道
我认为 执行interrupt 会抛出一个中断异常。而且中断线程就是靠这个异常机制执行的。如果把这段代码TRY CATCH了。中断当然就停止了。

我也这么认为。。
0 请登录后投票
   发表时间:2011-10-20  
对于没有处于阻塞状态(wait/sleep/join等)的线程,java不能真正的终止线程,需要你自己在代码里判断Thread.interrupted(),然后抛出自己的异常,相当于线程终止。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics