Custom Thread Pool in Java

Introduction In this tutorial I am going to show how to create custom thread pool in Java. Multi-threaded and multi-process programmings are great ways to optimize CPU usage and get things done quickly. What is thread pool? Thread pool is a collection of already created worker threads ready to perform certain tasks. Instead of creating and discarding thread once the task is done, thread pool reuses the thread in the form of worker thread. The worker thread differently exists from Runnable or Callable tasks it executes and often used to…

Continue

Java CyclicBarrier

A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point. CyclicBarriers are useful in programs involving a fixed sized party of threads that must occasionally wait for each other. The barrier is called cyclic because it can be re-used after the waiting threads are released. A CyclicBarrier supports an optional Runnable command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released. This barrier action is useful for…

Continue

Java CountDownLatch

What is CountDownLatch? A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes, i.e., a kind of synchronization tool that allows one Thread  to wait for one or more Threads before it starts processing. How does CountDownLatch work? A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the countDown() method, after which all waiting threads are released and any subsequent invocations of await return immediately.…

Continue

BlockingQueue in Java

A blocking queue is a queue that blocks when you try to dequeue an empty queue, or if you try to enqueue items in a full queue. A thread trying to dequeue an empty queue is blocked until some other threads insert an item into the queue. A thread trying to enqueue an item in a full queue is blocked until some other threads make space available in the queue, either by dequeuing one or more items or clearing the queue completely. It means a queue that additionally supports operations…

Continue

How to concurrently execute tasks using Java in Spring

This tutorials will show you how we can concurrently execute different operations using Java utility class called java.util.concurrent.Executors in Spring framework. Spring supports XML as well as Programmatic configuration of beans but, here we will use XML based metadata configuration. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse. Prerequisites The following things are required in order to run the application Eclipse Kepler JDK 1.8 Have maven…

Continue

How ThreadLocal works in Java

Introduction We will see how ThreadLocal works in Java. Java ThreadLocal class is used to create thread-local variables. The thread-local variable can be read and written by the same thread only. In multi-threaded environments all of the threads of an Object share its variables, so if the variable is not thread safe, then we will get inconsistent state of the variable. Therefore we can use synchronization, but if we want to avoid synchronization, we can use ThreadLocal class to create thread-local variables. Every thread has its own ThreadLocal variable and…

Continue