Choose between ExecutorService’s submit and ExecutorService’s execute

How should I choose between ExecutorService’s submit or execute, if the returned value is not my concern? If I test both, I didn’t see any differences among the two except the returned value. ExecutorService threadExecutor = Executors.newSingleThreadExecutor(); threadExecutor.execute(new Task()); ExecutorService threadExecutor = Executors.newSingleThreadExecutor(); threadExecutor.submit(new Task()); 7 Answers 7

ExecutorService, how to wait for all tasks to finish

What is the simplest way to to wait for all tasks of ExecutorService to finish? My task is primarily computational, so I just want to run a large number of jobs – one on each core. Right now my setup looks like this: ExecutorService es = Executors.newFixedThreadPool(2); for (DataTable singleTable : uniquePhrases) { es.execute(new ComputeDTask(singleTable)); … Read more

Handling exceptions from Java ExecutorService tasks

I’m trying to use Java’s ThreadPoolExecutor class to run a large number of heavy weight tasks with a fixed number of threads. Each of the tasks has many places during which it may fail due to exceptions. I’ve subclassed ThreadPoolExecutor and I’ve overridden the afterExecute method which is supposed to provide any uncaught exceptions encountered … Read more

Naming threads and thread-pools of ExecutorService

Let’s say I have an application that utilizes the Executor framework as such Executors.newSingleThreadExecutor().submit(new Runnable(){ @Override public void run(){ // do stuff } } When I run this application in the debugger, a thread is created with the following (default) name: Thread[pool-1-thread-1]. As you can see, this isn’t terribly useful and as far as I … Read more

How to wait for all threads to finish, using ExecutorService?

I need to execute some amount of tasks 4 at a time, something like this: ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(…) { taskExecutor.execute(new MyTask()); } //…wait for completion somehow How can I get notified once all of them are complete? For now I can’t think about anything better than setting some global task counter and decrease … Read more