1

Why main thread is not stopped when one of the thread from threadpool throws RejectedExecutionException ?Am i doing something wrong here? 3rd thread in the threadpool is throwing RejectedExecutionException and i am not handling this exception in main thread. So do i necessarily handle this exception to make my main thread stop. Any help would be appreciated.

    public static void main(String[] args) {
        ExecutorService threadPool = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<Runnable>(1));
        threadPool.execute(new TestOne());
        threadPool.execute(new TestTwo());
        threadPool.execute(new TestThree());
        threadPool.shutdown();
        try {
            threadPool.awaitTermination(2L, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("main thread stopped");
    }
}

class TestOne implements Runnable {
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Executing task one");
            try {
                Thread.sleep(10);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }

    }
}

class TestTwo implements Runnable {
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Executing task two");
            try {
                Thread.sleep(10);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
}

class TestThree implements Runnable {
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println("Executing task three");
            try {
                Thread.sleep(10);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
}
Sunny
  • 127
  • 1
  • 9

1 Answers1

4

No threads from the thread pool are throwing a RejectedExecutionException. The main thread which is submitting the tasks to the thread pool is actually throwing the RejectedExecutionException.

The main thread will not block on submission as the spec's dictate it should not. There are mechanisms to do this, see ThreadPoolExecutor Block When Queue Is Full?

John Vint
  • 39,695
  • 7
  • 78
  • 108
  • Thanks @John, i got this. I have also noticed that, if i submit only two threads in thread pool for execution, main thread doesn't throw RejectedExecutionException. But here the size of blocking queue is one and even max pool size is also one. AFAIK if the queue is full and if we cannot add any more threads the above exception could be thrown. Then why does main thread throws exception only on 3rd thread execution and not on 2nd. – Sunny Jul 18 '17 at 18:10
  • 1
    A `RJE` will be thrown when all threads are active and the queue is full. You submit the first task, the only thread is active and queue is now empty. You submit the second task, the thread is active and queue is full. Third task throws the `RJE`. You can read my answer here, https://stackoverflow.com/questions/19003430/what-are-the-possible-reason-for-a-java-util-concurrent-rejectedexecutionexcepti/19006386#19006386. – John Vint Jul 18 '17 at 18:29
  • Thanks John.This is useful. – Sunny Jul 18 '17 at 18:37
  • @Sunny Glad to help! – John Vint Jul 18 '17 at 19:15