Thinking in Java学习笔记,可以被Future.cancel()中断的资源

线程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 stub
		test(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{
	@Override
	public 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;
	}
	@Override
	public 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();
	}
	@Override
	public 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 stub
		ExecutorService 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;
	}
	@Override
	public 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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值