• Java ExecutorService example using Callable and Future
    09 January

    Java ExecutorService example using Callable and Future

    Callable interface represents a thread that can return a value. It is very much similar to Runnable interface except that it can return a value. Callable interface can be used to compute status or results that can be returned to invoking thread. For example: Let’s say you want to perform factorial and square of some […]

  • Java Exchanger example
    06 January

    Java Exchanger example

    Exchanger class is introduced with java 1.5 with other classes such ConcurrentHashMap, CountDownLatch, Semaphores. Exchanger class is used to exchange object between two threads. Exchanger simply waits until two separate threads calls exchange method, when it happens, it exchanges data supplied by threads.Two threads can pair and swap objects between them. Exchanger class may be useful […]

  • 05 January

    Java newCachedThreadPool Example

    1. Introduction Java’s Executors.newCachedThreadPool() factory method provides a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. Our goal is to understand how to use this thread pool to execute asynchronous tasks efficiently. 2. What is newCachedThreadPool? The newCachedThreadPool method from the java.util.concurrent.Executors class is a […]

  • 27 December

    Java ThreadPoolExecutor

    Java 5 has introduced new concurrent API called Executor frameworks to make programmer life easy. It simplifies design and development of multi-thread applications. It consists of mainly Executor, ExecutorService interface and ThreadPoolExecutor class which implements both interfaces i.e. Executor and ExecutorService. ThreadPoolExecutor class provide the implementation of thread pool. We will understand more about it […]

  • Java Semaphore example
    23 December

    Java Semaphore example

    In this tutorial, we are going to see about Semaphore in java. Semaphore is a class in java.util.concurrent package introduced in JDK 5. Semaphore basically maintains a set of permits, so there are two methods which are mainly used for semaphore. acquire release acquire() method is used to get a permit and if no. of […]

  • Difference between CountDownLatch and CyclicBarrier
    11 July

    Difference between CountDownLatch and CyclicBarrier in java

    In this post, we will see difference between CountDownLatch and CyclicBarrier in java. Lets first see similarities among them. Both wait for specific number of threads or tasks to finish. Lets see differences between CountDownLatch and CyclicBarrier. CountDownLatch vs CyclicBarrier Parameter CountDownLatch CyclicBarrier Reuse It can not be reused once count reaches 0 It can […]

  • CountDownLatch in java
    08 August

    CountDownLatch in java

    As per java docs, CountDownLatch is synchronisation aid that allows one or more threads to wait until set of operations being performed in other threads completes.

  • ConcurrentHashMap in java
    26 November

    ConcurrentHashMap in java

    ConcurrentHashMap was introduced in Java 5 with other concurrency utils such as CountDownLatch, CyclicBarrier and BlockingQueue. ConcurrentHashMap in java is very similar to HashTable but it provides better concurrency level. You might know , you can synchonize HashMap using Collections.synchronizedMap(Map). So what is difference between ConcurrentHashMap and Collections.synchronizedMap(Map)In case of Collections.synchronizedMap(Map), it locks whole HashTable […]