当前位置:编程学习 > JAVA >>

interrupt的疑问

public class ThreadTest {

public static void main(String[] args) {

try {
System.out.println("try");
Thread thread = new MyThread();
thread.start();
thread.interrupt();
} catch (Exception e) {
System.out.println("1exception");
} finally {
System.out.println("finally");
}
}
}

class MyThread extends Thread {

public void run() {
try {
System.out.println("run");
for(int i=0;i<10000;i++)
System.out.print(i);
System.out.println();
Thread.sleep(1000);
throw new Exception();
} catch (Exception e) {
System.out.println("2exception ");
}
}
}


输出:
try
finally
run
0123456789
2exception 

求高人解释下这个线程可能执行过程?明明对子线程interrupted()了为什么子线程还是全部都执行了?
--------------------编程问答--------------------
引用 楼主 mingzidaodiduochang 的回复:
public class ThreadTest {

public static void main(String[] args) {

try {
System.out.println("try");
Thread thread = new MyThread();
thread.start();
thread.interrupt();
} catch (Exception e) {
System.out.println("1exception");
} finally {
System.out.println("finally");
}
}
}

class MyThread extends Thread {

public void run() {
try {
System.out.println("run");
for(int i=0;i<10000;i++)
System.out.print(i);
System.out.println();
Thread.sleep(1000);
throw new Exception();
} catch (Exception e) {
System.out.println("2exception ");
}
}
}


输出:
try
finally
run
0123456789
2exception 

求高人解释下这个线程可能执行过程?明明对子线程interrupted()了为什么子线程还是全部都执行了?


interrupt() 是用来跳出循环的

public void run(){
  while(!interrupted()){
  . . . 
  }
} --------------------编程问答-------------------- sleep() --------------------编程问答-------------------- interrupt() 只是修改了线程的中断标志位。是不会结束程序的。不然它与stop 一样就会发生死锁之类的问题。
interrupt():方法主要是为了取消当前执行的任务,由用户取消而不是由系统来帮你终止。这样就可以更好的管理资源,释放锁这些操作。 --------------------编程问答--------------------
引用 3 楼 PHIILL_01 的回复:
interrupt() 只是修改了线程的中断标志位。是不会结束程序的。不然它与stop 一样就会发生死锁之类的问题。
interrupt():方法主要是为了取消当前执行的任务,由用户取消而不是由系统来帮你终止。这样就可以更好的管理资源,释放锁这些操作。

public class ThreadTest {

public static void main(String[] args) {

try {
System.out.println("try");
Thread thread = new MyThread();
thread.start();
thread.interrupt();
} catch (Exception e) {
System.out.println("1exception");
} finally {
System.out.println("finally");
}
}
}

class MyThread extends Thread {

public void run() {
try {
System.out.println("run");
Thread.sleep(100000); //多加了这行
System.out.println("addition");

throw new Exception();
} catch (Exception e) {
System.out.println("2exception ");
}
}
}

输出:
try
finally
run
2exception 


这个到底有什么不同?
可以解释下着两个程度的执行过程么? --------------------编程问答-------------------- 你用错了,
如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。
中断一个不处于活动状态的线程不需要任何作用。
比如.你新线程中用sleep(int)
你在main线程中打断它,新线程sleep会抛InterruptedException
线程好好学,很有意思,加油吧 --------------------编程问答--------------------
引用 5 楼 qq331077064 的回复:
你用错了,
如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。
中断一个不处于活动状态的线程不需要任何作用。
比如.你新线程中用sleep(int)
你在main线程中打断它,新线程sleep会抛InterruptedException
线程好好学,很有意思,加油吧
可以说下产生这个结果的大概执行过程么。 --------------------编程问答-------------------- 兄弟,你用法就错了好吧。这个事中断线程的sleep用的给你写了个简单的例子看看


public class Test4 {

public static void main(String[] args) throws Exception{
TestThread t = new TestThread();
t.start();
Thread.sleep(3000);// 睡3秒就唤醒线程
t.interrupt();
}
}

class TestThread extends Thread{

@Override
public void run() {
for(int i = 0; i < 5; i ++){
try {
Thread.sleep(1000);
System.out.println(i);
} catch (InterruptedException e) {
break;
}
}
System.out.println("TestThread goon...");
}
}

--------------------编程问答-------------------- 线程被你显示停下来了,你在另一个线程中打断它,那些停下的方法,比如join(),会有非运行时异常InterruptedException 抛出
补充:Java ,  Java SE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,