What is the difference between ExecutorService.submit and ExecutorService.execute in this code in Java?

As you see from the JavaDoc execute(Runnable) does not return anything. However, submit(Callable<T>) returns a Future object which allows a way for you to programatically cancel the running thread later as well as get the T that is returned when the Callable completes. See JavaDoc of Future for more details Future<?> future = executor.submit(longRunningJob); … … Read more

FixedThreadPool vs CachedThreadPool: the lesser of two evils

A CachedThreadPool seems appropriate for your situation as there are no negative consequence to using one for long running threads directly. The comment in the java doc about CachedThreadPools being suitable for short tasks merely suggest that they are particularly appropriate for such cases, not that they cannot be used for long running tasks. The … Read more

Does async(launch::async) in C++11 make thread pools obsolete for avoiding expensive thread creation?

Question 1: I changed this from the original because the original was wrong. I was under the impression that Linux thread creation was very cheap and after testing I determined that the overhead of function call in a new thread vs. a normal one is enormous. The overhead for creating a thread to handle a … Read more

Thread vs ThreadPool

Thread pool will provide benefits for frequent and relatively short operations by Reusing threads that have already been created instead of creating new ones (an expensive process) Throttling the rate of thread creation when there is a burst of requests for new work items (I believe this is only in .NET 3.5) If you queue … Read more

Thread pooling in C++11

This is adapted from my answer to another very similar post. Let’s build a ThreadPool class: class ThreadPool { public: void Start(); void QueueJob(const std::function<void()>& job); void Stop(); void busy(); private: void ThreadLoop(); bool should_terminate = false; // Tells threads to stop looking for jobs std::mutex queue_mutex; // Prevents data races to the job queue … Read more

ExecutorService, how to wait for all tasks to finish

The simplest approach is to use ExecutorService.invokeAll() which does what you want in a one-liner. In your parlance, you’ll need to modify or wrap ComputeDTask to implement Callable<>, which can give you quite a bit more flexibility. Probably in your app there is a meaningful implementation of Callable.call(), but here’s a way to wrap it … Read more