您好,欢迎来到爱问旅游网。
搜索
您的当前位置:首页Thinking in Java学习笔记,可以被Future.cancel()中断的资源

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---------------------");
	}
}


因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- awee.cn 版权所有 湘ICP备2023022495号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务