interrupt的疑问

名字到底多长 2013-10-10 09:39:43
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()了为什么子线程还是全部都执行了?
...全文
267 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
郑涛 2013-10-14
  • 打赏
  • 举报
回复
线程被你显示停下来了,你在另一个线程中打断它,那些停下的方法,比如join(),会有非运行时异常InterruptedException 抛出
安特矮油 2013-10-12
  • 打赏
  • 举报
回复
兄弟,你用法就错了好吧。这个事中断线程的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...");
	}
}

名字到底多长 2013-10-12
  • 打赏
  • 举报
回复
引用 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 线程好好学,很有意思,加油吧
可以说下产生这个结果的大概执行过程么。
名字到底多长 2013-10-11
  • 打赏
  • 举报
回复
引用 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 
这个到底有什么不同? 可以解释下着两个程度的执行过程么?
  • 打赏
  • 举报
回复
interrupt() 只是修改了线程的中断标志位。是不会结束程序的。不然它与stop 一样就会发生死锁之类的问题。 interrupt():方法主要是为了取消当前执行的任务,由用户取消而不是由系统来帮你终止。这样就可以更好的管理资源,释放锁这些操作。
郑涛 2013-10-11
  • 打赏
  • 举报
回复
你用错了, 如果线程在调用 Object 类的 wait()、wait(long) 或 wait(long, int) 方法,或者该类的 join()、join(long)、join(long, int)、sleep(long) 或 sleep(long, int) 方法过程中受阻,则其中断状态将被清除,它还将收到一个 InterruptedException。 中断一个不处于活动状态的线程不需要任何作用。 比如.你新线程中用sleep(int) 你在main线程中打断它,新线程sleep会抛InterruptedException 线程好好学,很有意思,加油吧
临碣 2013-10-10
  • 打赏
  • 举报
回复
sleep()
无聊找乐 2013-10-10
  • 打赏
  • 举报
回复
引用 楼主 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()){ . . . } }

62,616

社区成员

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

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