线程的中断是什么意思?

u013032535 2013-12-17 03:26:37
执行Thread类的interrupt()后,相当于run()里面的下面的部分不执行了,return?还是相当于Thread .sleep()?
如果这个线程里面有个GUI窗口——Jframe1,怎么能让这个线程终止,进入可以被gc回收的状态?Jframe1.dispose()可否?
...全文
253 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
u013032535 2013-12-18
  • 打赏
  • 举报
回复
引用 9 楼 u013192985 的回复:
Check whether the thread has been interrupted in the run method,if so, mannually write some codes to close your GUI window. About how to terminate a JFrame window, please refer to relevant API. Any questions, please let me know.
the relevant question is how to terminate a JFrame window. the dispos() method ?
reentrantlock 2013-12-18
  • 打赏
  • 举报
回复
Check whether the thread has been interrupted in the run method,if so, mannually write some codes to close your GUI window. About how to terminate a JFrame window, please refer to relevant API. Any questions, please let me know.
u013032535 2013-12-18
  • 打赏
  • 举报
回复
引用 7 楼 u013192985 的回复:
The interrupt method of class Thread never helps you terminate immediately the running of the thread.It only sets a flag which you can make use of to make logic judgement in the code. In order to implement the fuction of terminating the thread after calling the interrupt method,you can refer to the following code: public static void main(String[] args) throws Exception { Thread t1 = new Thread(new Runnable(){ @Override public void run() { System.out.println("task start..."); for( int k = 0;k<100;k++){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if(Thread.currentThread().isInterrupted()){ System.out.println("interrupt"); break; // exit the loop }else{ System.out.println("k:"+k); } } System.out.println("task end ..."); } }); t1.start(); Thread.sleep(5000); t1.interrupt(); Thread.sleep(100000); }
线程里面有个GUI窗口怎么办?
u013032535 2013-12-18
  • 打赏
  • 举报
回复
哈哈哈,实验了,就是.dispose();!
reentrantlock 2013-12-17
  • 打赏
  • 举报
回复
The interrupt method of class Thread never helps you terminate immediately the running of the thread.It only sets a flag which you can make use of to make logic judgement in the code. In order to implement the fuction of terminating the thread after calling the interrupt method,you can refer to the following code: public static void main(String[] args) throws Exception { Thread t1 = new Thread(new Runnable(){ @Override public void run() { System.out.println("task start..."); for( int k = 0;k<100;k++){ try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } if(Thread.currentThread().isInterrupted()){ System.out.println("interrupt"); break; // exit the loop }else{ System.out.println("k:"+k); } } System.out.println("task end ..."); } }); t1.start(); Thread.sleep(5000); t1.interrupt(); Thread.sleep(100000); }
u013032535 2013-12-17
  • 打赏
  • 举报
回复
线程里面有个GUI窗口怎么办?要是有50个窗口,那么50个县城都不退出?
u013032535 2013-12-17
  • 打赏
  • 举报
回复
引用 2 楼 Gaowen_HAN 的回复:
引用 1 楼 ygycomon 的回复:
interrupt是给线程打上一个状态,意思是告诉线程:童鞋,你要结束了。但是线程不会在任意地方就返回,也没有必要任意地方返回,因为大部分的操作都很快就完成了,只有小部分还需要花上不知道多长时间的操作,才需要被中断,这个长时间的操作,就是阻塞,如磁盘io阻塞、网络io阻塞、同步阻塞等等,这些API调用的时候都要捕获异常就是这个原因,一旦被打上interrupt状态以后,线程执行到可中断阻塞(有些中断是不可以被中断的)的时候,就会抛出异常并且结束当前的操作,然后重置状态
还是设置boolean型isCancelled好些,很安全
Thread类好像没有isCancelled。
fearlessMore 2013-12-17
  • 打赏
  • 举报
回复
引用 3 楼 ygycomon 的回复:
引用 2 楼 Gaowen_HAN 的回复:
[quote=引用 1 楼 ygycomon 的回复:] interrupt是给线程打上一个状态,意思是告诉线程:童鞋,你要结束了。但是线程不会在任意地方就返回,也没有必要任意地方返回,因为大部分的操作都很快就完成了,只有小部分还需要花上不知道多长时间的操作,才需要被中断,这个长时间的操作,就是阻塞,如磁盘io阻塞、网络io阻塞、同步阻塞等等,这些API调用的时候都要捕获异常就是这个原因,一旦被打上interrupt状态以后,线程执行到可中断阻塞(有些中断是不可以被中断的)的时候,就会抛出异常并且结束当前的操作,然后重置状态
还是设置boolean型isCancelled好些,很安全
interrupt是操作系统级别的支持[/quote]恩,实际使用中还是设置boolean标记好些
致知Fighting 2013-12-17
  • 打赏
  • 举报
回复
引用 2 楼 Gaowen_HAN 的回复:
引用 1 楼 ygycomon 的回复:
interrupt是给线程打上一个状态,意思是告诉线程:童鞋,你要结束了。但是线程不会在任意地方就返回,也没有必要任意地方返回,因为大部分的操作都很快就完成了,只有小部分还需要花上不知道多长时间的操作,才需要被中断,这个长时间的操作,就是阻塞,如磁盘io阻塞、网络io阻塞、同步阻塞等等,这些API调用的时候都要捕获异常就是这个原因,一旦被打上interrupt状态以后,线程执行到可中断阻塞(有些中断是不可以被中断的)的时候,就会抛出异常并且结束当前的操作,然后重置状态
还是设置boolean型isCancelled好些,很安全
interrupt是操作系统级别的支持
fearlessMore 2013-12-17
  • 打赏
  • 举报
回复
引用 1 楼 ygycomon 的回复:
interrupt是给线程打上一个状态,意思是告诉线程:童鞋,你要结束了。但是线程不会在任意地方就返回,也没有必要任意地方返回,因为大部分的操作都很快就完成了,只有小部分还需要花上不知道多长时间的操作,才需要被中断,这个长时间的操作,就是阻塞,如磁盘io阻塞、网络io阻塞、同步阻塞等等,这些API调用的时候都要捕获异常就是这个原因,一旦被打上interrupt状态以后,线程执行到可中断阻塞(有些中断是不可以被中断的)的时候,就会抛出异常并且结束当前的操作,然后重置状态
还是设置boolean型isCancelled好些,很安全
致知Fighting 2013-12-17
  • 打赏
  • 举报
回复
interrupt是给线程打上一个状态,意思是告诉线程:童鞋,你要结束了。但是线程不会在任意地方就返回,也没有必要任意地方返回,因为大部分的操作都很快就完成了,只有小部分还需要花上不知道多长时间的操作,才需要被中断,这个长时间的操作,就是阻塞,如磁盘io阻塞、网络io阻塞、同步阻塞等等,这些API调用的时候都要捕获异常就是这个原因,一旦被打上interrupt状态以后,线程执行到可中断阻塞(有些中断是不可以被中断的)的时候,就会抛出异常并且结束当前的操作,然后重置状态

62,615

社区成员

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

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