相关疑难解决方法(0)

处理Java ExecutorService任务中的异常

我正在尝试使用Java的ThreadPoolExecutor类来运行具有固定数量线程的大量重量级任务.每个任务都有许多地方,在这些地方可能因异常而失败.

我已经进行了子类化,ThreadPoolExecutor并且我已经覆盖了该afterExecute方法,该方法应该在运行任务时提供任何未捕获的异常.但是,我似乎无法使其发挥作用.

例如:

public class ThreadPoolErrors extends ThreadPoolExecutor {
    public ThreadPoolErrors() {
        super(  1, // core threads
                1, // max threads
                1, // timeout
                TimeUnit.MINUTES, // timeout units
                new LinkedBlockingQueue<Runnable>() // work queue
        );
    }

    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        if(t != null) {
            System.out.println("Got an error: " + t);
        } else {
            System.out.println("Everything's fine--situation normal!");
        }
    }

    public static void main( String [] args) {
        ThreadPoolErrors threadPool = new …
Run Code Online (Sandbox Code Playgroud)

java multithreading exception executorservice threadpoolexecutor

201
推荐指数
6
解决办法
15万
查看次数

在ExecutorService的提交和ExecutorService的执行之间进行选择

如果返回的值不是我关注的话,我应该如何在ExecutorService的 提交执行之间进行选择?

如果我同时测试两者,除了返回值之外,我没有看到两者之间有任何差异.

ExecutorService threadExecutor = Executors.newSingleThreadExecutor();
threadExecutor.execute(new Task());
Run Code Online (Sandbox Code Playgroud)
ExecutorService threadExecutor = Executors.newSingleThreadExecutor();
threadExecutor.submit(new Task());
Run Code Online (Sandbox Code Playgroud)

java multithreading executorservice

187
推荐指数
5
解决办法
9万
查看次数

为什么ExecutorService不调用UncaughtExceptionHandler?

我偶然发现了一个问题,可归纳如下:

当我手动创建线程(即通过实例化java.lang.Thread)时,UncaughtExceptionHandler适当地调用.但是,当我使用ExecutorService带有ThreadFactory处理程序时,ommited.我错过了什么?

public class ThreadStudy {

private static final int THREAD_POOL_SIZE = 1;

public static void main(String[] args) {

    // create uncaught exception handler

    final UncaughtExceptionHandler exceptionHandler = new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            synchronized (this) {
                System.err.println("Uncaught exception in thread '" + t.getName() + "': " + e.getMessage());
            }
        }
    };

    // create thread factory

    ThreadFactory threadFactory = new ThreadFactory() {

        @Override
        public Thread newThread(Runnable r) …
Run Code Online (Sandbox Code Playgroud)

java multithreading

55
推荐指数
5
解决办法
3万
查看次数

使用ThreadPoolExecutor提交和执行方法有什么区别

我发现有两种方法(提交和执行)将Runnable添加到线程池中,有什么区别?

java multithreading

31
推荐指数
2
解决办法
4万
查看次数

"由于故障导致的线程终止"是指什么?

javadoc ExecutorService有时指的是Thread因"失败"而终止的情况.但是,目前尚不清楚这是指什么样的失败.

例如,单线程执行程序文档说明了这一点

如果这个单线程由于在关闭之前执行期间的故障而终止,那么如果需要执行后续任务,则新线程将取代它

我原以为这种情况可能发生在Exception或者a RuntimeException的情况下,但似乎并非如此.运行以下代码似乎给出了相同的线程名称和线程ID.

ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(() -> {
  System.out.println("Hello from " + Thread.currentThread().getName()+ " " + Thread.currentThread().getId());
  throw new NullPointerException("Test");
});

executor.submit(() -> {
  System.out.println("Hello 2 from " + Thread.currentThread().getName() + " " + Thread.currentThread().getId());
});
Run Code Online (Sandbox Code Playgroud)

此代码的输出是:

Hello from pool-1-thread-1 12
Hello 2 from pool-1-thread-1 12
Run Code Online (Sandbox Code Playgroud)

似乎即使在这种情况下,同一个线程也被重用NullPointerException.

那么Javadoc指的是什么样的"失败"?

java multithreading executorservice

6
推荐指数
1
解决办法
293
查看次数