《Thinking in Java》读书笔记之并发(五)

来源:互联网 发布:树洞表白墙免费源码 编辑:程序博客网 时间:2024/05/21 20:24

让步

如果知道已经完成了在run()方法的循环的一次迭代过程中所需的工作,就可以给线程调度机制一个暗示:你的工作已近做的差不多了,可以让别的线程使用CPU了,这个暗示将通过调用yield()方法作出(不过这只是一个暗示,没有任何机制保证它将被采纳),当调用yield()时,你也是在建议具有相同优先级的其他线程可以运行

后台线程

后台线程(deamon)线程,又叫守护线程,是指程序运行的时候在后台提供的一种通用服务线程。这种线程不属于程序中不可或缺的部分,因此,当所有非后台线程结束时,程序也就终止了,同时会杀死进程中的所有后台线程

必须在线程启动调用setDeamon()方法,才能把它设置为后台线程

例程:

//: concurrency/SimpleDaemons.java// Daemon threads don't prevent the program from ending.import java.util.concurrent.*;import static net.mindview.util.Print.*;public class SimpleDaemons implements Runnable {  public void run() {    try {      while(true) {        TimeUnit.MILLISECONDS.sleep(100);        print(Thread.currentThread() + " " + this);      }    } catch(InterruptedException e) {      print("sleep() interrupted");    }  }  public static void main(String[] args) throws Exception {    for(int i = 0; i < 10; i++) {      Thread daemon = new Thread(new SimpleDaemons());      daemon.setDaemon(true); // Must call before start()      daemon.start();    }    print("All daemons started");    TimeUnit.MILLISECONDS.sleep(175);  }} /* Output: (Sample)All daemons startedThread[Thread-0,5,main] SimpleDaemons@530daaThread[Thread-1,5,main] SimpleDaemons@a62fc3Thread[Thread-2,5,main] SimpleDaemons@89ae9eThread[Thread-3,5,main] SimpleDaemons@1270b73Thread[Thread-4,5,main] SimpleDaemons@60aeb0Thread[Thread-5,5,main] SimpleDaemons@16caf43Thread[Thread-6,5,main] SimpleDaemons@66848cThread[Thread-7,5,main] SimpleDaemons@8813f2Thread[Thread-8,5,main] SimpleDaemons@1d58aaeThread[Thread-9,5,main] SimpleDaemons@83cc67...*///:~

可以调用isDeamon()方法来确定线程是否是一个后台线程,如果是一个后台线程,那么它创建的任何线程将被自动设置成后台线程

当最后一个非后台线程终止时,后台线程会“突然”终止。

加入一个线程

一个线程在其他线程上调用join()方法,其效果是等待一段时间直到第二个线程结束才继续执行,如果某个线程在另一个线程t上调用t.join(),此线程将被挂起。直到目标线程t结束才恢复

也可以在调用join()时带上一个超时参数,这样如果目标线程在这段时间到期时还没有结束的话,join()方法总能返回。

对join()方法的调用可以被中断,做法是在线程上调用interrupt()方法,这需要用到try-catch子句

异常捕获

由于线程的本质特性,使得你不能捕获从线程中逃逸的异常,一旦异常逃逸任务的run()方法,他就会向外传播到控制台,除非你采取特殊的步骤捕获这种错误的异常

Thread.UncaughtExceptionHandler是Java SE5中的新接口 ,它允许你在每个Thread对象上附着一个异常处理器

Thread.UncaughtExceptionHandler的uncaughtException()方法会在线程因未捕获的异常而临近死亡时被调用。

示例代码:

//: concurrency/CaptureUncaughtException.javaimport java.util.concurrent.*;class ExceptionThread2 implements Runnable {  public void run() {    Thread t = Thread.currentThread();    System.out.println("run() by " + t);    System.out.println(      "eh = " + t.getUncaughtExceptionHandler());    throw new RuntimeException();  }}class MyUncaughtExceptionHandler implementsThread.UncaughtExceptionHandler {  public void uncaughtException(Thread t, Throwable e) {    System.out.println("caught " + e);  }}class HandlerThreadFactory implements ThreadFactory {  public Thread newThread(Runnable r) {    System.out.println(this + " creating new Thread");    Thread t = new Thread(r);    System.out.println("created " + t);    t.setUncaughtExceptionHandler(      new MyUncaughtExceptionHandler());    System.out.println(      "eh = " + t.getUncaughtExceptionHandler());    return t;  }}public class CaptureUncaughtException {  public static void main(String[] args) {    ExecutorService exec = Executors.newCachedThreadPool(      new HandlerThreadFactory());    exec.execute(new ExceptionThread2());  }} /* Output: (90% match)HandlerThreadFactory@de6ced creating new Threadcreated Thread[Thread-0,5,main]eh = MyUncaughtExceptionHandler@1fb8ee3run() by Thread[Thread-0,5,main]eh = MyUncaughtExceptionHandler@1fb8ee3caught java.lang.RuntimeException*///:~
0 0
原创粉丝点击