What’s the advantage of a Java-5 ThreadPoolExecutor over a Java-7 ForkJoinPool?

ThreadPool (TP) and ForkJoinPool (FJ) are targeted towards different use cases. The main difference is in the number of queues employed by the different executors which decide what type of problems are better suited to either executor. The FJ executor has n (aka parallelism level) separate concurrent queues (deques) while the TP executor has only … Read more

SingleThreadExecutor VS plain thread

Executors#newSingleThreadExecutor() creates ThreadPoolExecutor object under the hood, see the code here: http://www.docjar.com/html/api/java/util/concurrent/Executors.java.html 133 public static ExecutorService newSingleThreadExecutor() { 134 return new FinalizableDelegatedExecutorService 135 (new ThreadPoolExecutor(1, 1, 136 0L, TimeUnit.MILLISECONDS, 137 new LinkedBlockingQueue<Runnable>())); 138 } The documentation of ThreadPoolExecutor explains in what situations it gives advantages: Thread pools address two different problems: they usually provide improved … Read more

ExecutorService vs ThreadPoolExecutor using LinkedBlockingQueue

Here is the source of Executors.newFixedThreadPool: public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } It internally uses ThreadPoolExecutor class with default configuration as you can see above. Now there are scenarios where default configuration is not suitable say instead of LinkedBlockingQueue a priority queue needs to be used … Read more

How to wait for all tasks in an ThreadPoolExecutor to finish without shutting down the Executor?

If you are interested in knowing when a certain task completes, or a certain batch of tasks, you may use ExecutorService.submit(Runnable). Invoking this method returns a Future object which may be placed into a Collection which your main thread will then iterate over calling Future.get() for each one. This will cause your main thread to … Read more

How to get the ThreadPoolExecutor to increase threads to max before queueing?

How can I work around this limitation in ThreadPoolExecutor where the queue needs to be bounded and full before more threads will be started. I believe I have finally found a somewhat elegant (maybe a little hacky) solution to this limitation with ThreadPoolExecutor. It involves extending LinkedBlockingQueue to have it return false for queue.offer(…) when … Read more

Impossible to make a cached thread pool with a size limit?

The ThreadPoolExecutor has the following several key behaviors, and your problems can be explained by these behaviors. When tasks are submitted, If the thread pool has not reached the core size, it creates new threads. If the core size has been reached and there is no idle threads, it queues tasks. If the core size … Read more

Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()

I think the docs explain the difference and usage of these two functions pretty well: newFixedThreadPool Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, … Read more

Handling exceptions from Java ExecutorService tasks

WARNING: It should be noted that this solution will block the calling thread in future.get(). If you want to process exceptions thrown by the task, then it is generally better to use Callable rather than Runnable. Callable.call() is permitted to throw checked exceptions, and these get propagated back to the calling thread: Callable task = … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)