线程终结

中断

先谈谈Thread.suspend, Thread.stop这些方法为什么被ban了,一个线程不应该由其它线程来强制中断或停止,而应该由线程自己本身的代码自行停止。所以java中并没有中断线程的语法,而是通过维护一个中断标志来让程序员自己来实现。

有关中断的API

  • void interrput()
  • static boolean interrupted()
  • boolean isInterrupted()
    interrput并不是中断调用此方法线程对象,而是改变此线程的中断标志位为true。
    interrupted是一个静态方法,该方法是复原中断标志位,并返回当前的中断位标志
    isInterrupted判断调用者线程的中断状态
public static boolean interrupted () {
     return currentThread().isInterrupted(true); //作用于当前线程
}
public boolean isInterrupted () {
     return isInterrupted( false);
}
private native boolean isInterrupted( boolean ClearInterrupted); //这个ClearInterrupted参数来判断是否清处标志位

关于正处于阻塞状态的中断

在Runnable.run()的运行过程中中断它,比较麻烦,可能需要清理一些资源,保证安全。所以在阻塞中断时,会抛出中断异常,让程序员捕捉处理后续逻辑。
ps:当抛出异常或Thread.interrupted(),中断位置复位

public class InterruptedTest {
    public static void main(String[] args) {
        Thread test = new Thread(new Runner8());
        test.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        test.interrupt();
    }
}

class Runner8 implements Runnable {

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            System.out.println(Thread.currentThread().isInterrupted()); // true
        }
        // System.out.println(Thread.interrupted()); 
        try {   //中断标记位true,就会捕捉;如果上面那句执行就不会被捕捉
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            System.out
                    .println("sleep" + Thread.currentThread().isInterrupted());
            System.out.println(e.getLocalizedMessage());
        }
        System.out.println(Thread.currentThread().isInterrupted()
                + " After while!");
    }
}

一般阻塞有这么几种

  • sleep
  • wait
  • I/O阻塞
  • synchronized
    下面引用<>的一段代码
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;

class SleepBlocked implements Runnable {
    @Override
    public void run() {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            System.out.println("InterruptedException");
        }
        System.out.println("Exiting SleepBlocked.run()");
    }
}

class IOBlocked implements Runnable {
    private InputStream in;
    public IOBlocked(InputStream is) {
        in = is;
    }
    @Override
    public void run() {
        try {
            System.out.println("Waiting for read():");
            in.read();
        } catch (IOException e) {
            if (Thread.currentThread().isInterrupted()) {
                System.out.println("Interrupted from Blocked I/O");
            } else {
                throw new RuntimeException(e);
            }
        }
        System.out.println("Exiting IOBlocked.run()");
    }
}

class SynchronizedBlocked implements Runnable {
    public synchronized void f() {
        while(true) {
            //永不释放获得的锁
            Thread.yield();
        }
    }
    public SynchronizedBlocked() {
        //在构造的时候就获取该对象的锁
        new Thread(){
            @Override
            public void run() {
                f();
            }
        };
    }
    
    @Override
    public void run() {
        System.out.println("Trying to call f()");
        f();
        System.out.println("Exiting SynchronizedBlocked.run()");
    }
}

public class Interrupting {
    private static ExecutorService exec = Executors.newCachedThreadPool();
    static void test(Runnable r) throws InterruptedException {
        Future future = exec.submit(r);
        TimeUnit.SECONDS.sleep(1);
        System.out.println("Interrupting " + r.getClass().getName());
        future.cancel(true);//如果在运行的话,中断该线程。
        System.out.println("Interrupting sent to " + r.getClass().getName());
    }
    
    public static void main(String[] args) throws Exception {
        test(new SleepBlocked());
        test(new IOBlocked(System.in));
        test(new SynchronizedBlocked());
        TimeUnit.SECONDS.sleep(3);
        System.out.println("Aborting with System.exit(0);");
        //强行停止退出
        System.exit(0);
    }
}
/*
执行结果:
Interrupting SleepBlocked
Interrupting sent to SleepBlocked
InterruptedException
Exiting SleepBlocked.run()
Waiting for read():
Interrupting IOBlocked
Interrupting sent to IOBlocked
Trying to call f()
Interrupting SynchronizedBlocked
Interrupting sent to SynchronizedBlocked
Aborting with System.exit(0);
*/

解析:synchronized对象锁的阻塞,由一个构造函数创建的一个匿名线程对象跑同步f(),自身也是一个线程类,

你可能感兴趣的:(线程终结)