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
原创粉丝点击