首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

封闭底层资源强行中断

2012-10-30 
关闭底层资源强行中断对于IO这种资源无法通过interrupt()方法中断,但是在实际应用中想中断的话,可以尝试关

关闭底层资源强行中断
对于IO这种资源无法通过interrupt()方法中断,但是在实际应用中想中断的话,可以尝试关闭任务在其上发生中断的底层资源。
=================

public class CloseResource {public static void main(String[] args) throws Exception{ExecutorService exec=Executors.newCachedThreadPool();ServerSocket server=new ServerSocket(8080);InputStream socketInput=new Socket("localhost",8080).getInputStream();exec.execute(new IOBlocked(socketInput));exec.execute(new IOBlocked(System.in));TimeUnit.MILLISECONDS.sleep(100);System.out.println("shut down all threads");exec.shutdownNow();TimeUnit.SECONDS.sleep(1);System.out.println("closing "+socketInput.getClass().getName());socketInput.close();TimeUnit.SECONDS.sleep(1);System.out.println("closing "+System.in.getClass().getName());System.in.close();}}

-----------------执行结果如下:
Waiting For Read...
Waiting For Read...
shut down all threads
closing java.net.SocketInputStream
Interrupted from blocked I/O
Exiting IOBlocked.run()
closing java.io.BufferedInputStream

热点排行