线程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---------------------");
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/38542.html