Thinking in Java学习笔记,可以被Future.cancel()中断的资源
来源:互联网 发布:linux 目录空间查看 编辑:程序博客网 时间:2024/06/06 16:36
线程sleep是可以被Future.cacel()中断的
线程中的IO阻塞时,线程无法被Future.cancel()中断
线程中的synchronized锁阻塞时,线程无法被Future.cancel()方法中断(Lock是可以被中断的)
package com.test.concurrent;import java.io.IOException;import java.io.InputStream;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;public class Interrupting {static ExecutorService exec=Executors.newCachedThreadPool();public static void main(String[] args) {// TODO Auto-generated method stubtest(new SleepBlocked());test(new IOBlocked(System.in));test(new SynchronizedBlocked());}public static void test(Runnable r){Future<?> f=exec.submit(r);System.out.println("prepare to cancel:"+r.getClass().getName());f.cancel(true);System.out.println("Interrupt sent to :"+r.getClass().getName());}}class SleepBlocked implements Runnable{@Overridepublic void run(){try{TimeUnit.MINUTES.sleep(4);//可被中断}catch(InterruptedException e){System.out.println("sleep interruption!!");}System.out.println("exiting SleepBlocked-------------------------");}}class IOBlocked implements Runnable{private InputStream in;public IOBlocked(InputStream in){this.in=in;}@Overridepublic void run(){try{System.out.println("prepare to read:::::::::");in.read();//IO阻塞,不可被线程中断}catch (IOException e) {// TODO Auto-generated catch block//e.printStackTrace();if(Thread.interrupted()){System.out.println("ioblocked is interrupted!!!");}else{System.out.println("ioblocked is not interrupted, just io exception");throw new RuntimeException();}}System.out.println("exiting IOBlocked---------------------");}}class SynchronizedBlocked implements Runnable{public SynchronizedBlocked(){new Thread(){public void run(){f();}}.start();}public synchronized void f(){while(true)Thread.yield();}@Overridepublic void run(){System.out.println("trying to call f()");f();//synchronized锁无法被线程中断System.out.println("exiting SynchronizedBlocked------------------------");}}
对于IO阻塞这种情况,可以通过关闭底层IO的方法,来中断线程的执行
package com.test.concurrent;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import java.util.concurrent.TimeUnit;public class Interrupting {static ExecutorService exec=Executors.newCachedThreadPool();public static void main(String[] args) throws IOException, InterruptedException {// TODO Auto-generated method stubExecutorService exec=Executors.newCachedThreadPool();ServerSocket server=new ServerSocket(8080);InputStream input=new Socket("localhost",8080).getInputStream();Future<?> f1=exec.submit(new IOBlocked(input));Future<?> f2=exec.submit(new IOBlocked(System.in));TimeUnit.SECONDS.sleep(3);System.out.println("trying to terminate all threads-----------");f1.cancel(true);f2.cancel(true);System.out.println("close socket:::::"+input.getClass().getName());input.close();TimeUnit.SECONDS.sleep(2);System.out.println("close input::::::"+System.in.getClass().getName());System.in.close();TimeUnit.SECONDS.sleep(2);}}class IOBlocked implements Runnable{private InputStream in;public IOBlocked(InputStream in){this.in=in;}@Overridepublic void run(){try{System.out.println("prepare to read:::::::::");in.read();//IO阻塞,不可被线程中断}catch (IOException e) {// TODO Auto-generated catch block//e.printStackTrace();if(Thread.interrupted()){System.out.println("ioblocked is interrupted!!!");}else{System.out.println("ioblocked is not interrupted, just io exception");throw new RuntimeException();}}System.out.println("exiting IOBlocked---------------------");}}
0 0
- Thinking in Java学习笔记,可以被Future.cancel()中断的资源
- Thinking in Java学习笔记 Semaphore控制的资源池
- Thinking in Java学习笔记,使用Exchanger交换资源
- Thinking In Java学习笔记
- Thinking in Java学习笔记
- thinking in java 学习笔记
- 《Thinking in java》学习笔记
- Thinking in Java学习笔记
- Thinking In Java学习笔记
- 我的Thinking in Java 3rd 的学习笔记
- java学习笔记1-thinking in java
- java学习笔记2-thinking in java
- java学习笔记3-thinking in java
- 我的Thinking in Java学习笔记(1)
- 我的Thinking in Java学习笔记(2)
- 我的Thinking in Java学习笔记(三)
- 我的Thinking in Java学习笔记(四)
- 我的Thinking in Java学习笔记(五)
- 如何查看linux系统下的各种日志文件 linux 系统日志的分析大全
- vi文本插入命令汇总
- (4.1.18.4)Android Service 服务(三)—— bindService与remoteService
- 有关内存的思考题
- tcpdump 高级应用
- Thinking in Java学习笔记,可以被Future.cancel()中断的资源
- HDU 2523--SORT AGAIN【水题】
- vlan原理详解,vlan功能及优点,vlan配制命令
- 堆 在游戏中的运用
- Qt5程序发布后运行,解决出现could not find or load the Qt Platform plugin windows错误
- Eclipse Java EE透视图下看不见Referenced Libraries解决办法
- vmware esxi 报错an error occurred while saving the snapshot:change tracking target file already exists
- linux下解压zip文件时,文件名乱码的解决
- (4.1.19)【Android UI设计与开发】第06期:底部菜单栏(一)使用TabActivity实现底部菜单栏