Is there a way to inadvertently stop the threads of an Executor?

I tried crashing the Thread of a fixed thread pool of a standard ExecutorService with this horrible code, in order to test its resilience against exceptions and errors happening inside the tasks passed to the ExecutorService:

    ExecutorService executorService = Executors.newFixedThreadPool(1);
    executorService.execute(() -> {
        throw new RuntimeException("Trying to kill the thread");
    });
    Thread.sleep(100);
    executorService.execute(() -> {
        System.out.println("still executing");
    });

but the second task prints still executing so I think the exception is being caught or the Thread instance is restarted anyway. What is happening? Can I trust that no matter what happens inside a task (less than System.exit()) other submitted tasks will continue to be executed by the pool?

Answers


Your executor service uses a thread pool. If a thread is killed, one another will be instantiated to execute next task.

Your call Executors.newFixedThreadPool(1); set a limit on number of active threads only.

In order to stop executor service, you must call:

executorService.shutdown();

Need Your Help

Apache2 on Mac OSX: How to Get Shared Object Libs such as mod_authn_alias.so

macos apache apache2 shared-libraries

I am trying to start apache2, but I am missing some modules required by the config.

Grails static mapping for String[]

grails gorm

I am trying to store a string[] in a database with grails but tinyblob isn't large enough. I've tried changing the mapping to blob and longblob but when I do this I get an error: