What are the possible reason for a java.util.concurrent.RejectedExecutionException in a SingleThreadExecutor

前端 未结 4 521
执笔经年
执笔经年 2020-12-08 13:13

I create the following executor in a singleton:

   final private ExecutorService executor = Executors.newSingleThreadExecutor(new ThreadFactory() {
                 


        
相关标签:
4条回答
  • 2020-12-08 13:22

    You might have submitted tasks after calling executor.shutdown(). Normally to stop executor they do

        executor.shutdown();
        executor.awaitTermination(10, TimeUnit.MINUTES);
    
    0 讨论(0)
  • 2020-12-08 13:22

    Maybe you should use a thread pool instead of using a single executor.

        executor = new java.util.concurrent.ThreadPoolExecutor(30, 30, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() {
            final AtomicInteger threadNumber = new AtomicInteger( 1 );
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "Thread No : " + threadNumber.getAndIncrement());
            }
        });
    
    0 讨论(0)
  • 2020-12-08 13:32

    There are two reasons why execute would throw a RejectedExecutionException

    1. The queue is full and you cannot add any more threads
    2. The ThreadPool has been shutdown

    Since you are using a LinkedBlockingQueue the only way I can see this occurring is because you shutdown the pool.

    0 讨论(0)
  • 2020-12-08 13:43

    Threads are not available to execute the given task. No linked block queue to que task.

    0 讨论(0)
提交回复
热议问题