为什么缓存线程池会创建两个线程,并且为什么关闭它会改变这一点?

3

这是我的Java代码。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

class ExceptionThread2 implements Runnable {
    @Override
    public void run() {
        Thread t = Thread.currentThread();
        System.out.println("run() by " + t);
        System.out.println("eh = " + t.getUncaughtExceptionHandler());
        throw new RuntimeException();
    }
}

class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
    @Override
    public void uncaughtException(Thread t, Throwable e) {
        System.out.println("caught " + e);
    }
}

class HandlerThreadFactory implements ThreadFactory {
    @Override
    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());
        //exec.shutdown();
    }
}

结果如下:
com.concurrent.example.HandlerThreadFactory@7f31245a creating new Thread
created Thread[Thread-0,5,main]
eh = com.concurrent.example.MyUncaughtExceptionHandler@6d6f6e28
run() by Thread[Thread-0,5,main]
eh = com.concurrent.example.MyUncaughtExceptionHandler@6d6f6e28
com.concurrent.example.HandlerThreadFactory@7f31245a creating new Thread
created Thread[Thread-1,5,main]
eh = com.concurrent.example.MyUncaughtExceptionHandler@2870fdbb
caught java.lang.RuntimeException

当我取消注释//exec.shutdown()时,结果会有所不同。只创建了一个线程。
com.concurrent.example.HandlerThreadFactory@7f31245a creating new Thread
created Thread[Thread-0,5,main]
eh = com.concurrent.example.MyUncaughtExceptionHandler@6d6f6e28
run() by Thread[Thread-0,5,main]
eh = com.concurrent.example.MyUncaughtExceptionHandler@6d6f6e28
caught java.lang.RuntimeException

为什么?

主要区别在于它执行了这个方法两次,该方法名为 'newThread()'。 - bfq
2个回答

3
你的可运行线程崩溃了。因此,线程池会用新的线程代替它(为你可能提交的下一个任务)。如果线程池已经关闭,它将不会这样做。

1
当你执行以下操作时:
exec.execute(new ExceptionThread2());

创建一个工作线程并执行您的任务。

但是,如果它异常完成并抛出异常,则会死亡。

在这种情况下,线程池会创建另一个工作线程来替换已死亡的线程,因此您会看到两个created消息,但只有一个run()消息。

这是执行您的任务的runWorker

 * 1. We may start out with an initial task, in which case we
 * don't need to get the first one. Otherwise, as long as pool is
 * running, we get tasks from getTask. If it returns null then the
 * worker exits due to changed pool state or configuration
 * parameters.  Other exits result from exception throws in
 * external code, in which case completedAbruptly holds, which
 * usually leads processWorkerExit to replace this thread.

 final void runWorker(Worker w)
  • 其他退出是由外部代码中的异常抛出导致的,这种情况下completedAbruptly为真,通常会导致processWorkerExit替换此线程

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接