Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 862

Java Concurrency interview questions

$
0
0
  1. What is CountDownLatch in Java concurrency?

    CountDownLatch can be visualized as a latch that is released only after the given number of events occur. CountDownLatch is initialized with that count (given number of events).

    Each time one of those events occur count is decremented, for that countdown() method is used. Thread(s) that are waiting for the latch to release (current count reaches zero due to invocations of the countDown()method) are blocked using await() method.

    It is useful in the scenario when you want one or more threads to wait until one or more events being performed in other threads complete.

    Read more about CountDownLatch here.
  2. What is CyclicBarrier in Java concurrency?

    CyclicBarrier is useful in scenarios where you want set of threads to wait for each other to reach a common barrier point. When each thread reaches the barrier (common point) you need to call await() method on the CyclicBarrier object. This will suspend the thread until all the thread also call the await() method on the same CyclicBarrier object.

    Once all the specified threads have called await() method that will trip the barrier and all threads can resume operation.

    The barrier is called cyclic because it can be re-used after the waiting threads are released.

    Read more about CyclicBarrier here.
  3. What is the difference between a CountDownLatch and CyclicBarrier?

    • When you are using a CountDownLatch, you specify the number of calls to the countdown() method when creating a CountDownLatch object. What this means is you can use CountDownLatch with only a single thread and using countdown() to decrement as and when the specified even occur.
      When you are using CyclicBarrier you specify the number of threads that should call await() method in order to trip the barrier. That means if you have a CyclicBarrier initialized to 3 that means you should have at least 3 threads to call await().
    • CountDownLatch can't be reused, when count reaches zero it cannot be reset.
      CyclicBarrier can be reused after the waiting threads are released.
    Read more differences and explanation here.
  4. If a CountDownLatch is initialized with some count let's say 3 (new CountDownLatch(3)). Do we need to have 3 threads for countdown in that case?

    No. Same number of threads are not required. A CountDownLatch initialized to N can be used to make one thread wait until N threads have completed some action, or some action has been completed N times.

    Read more about CountDownLatch here.
  5. What is Phaser in Java concurrency?

    Phaser is more suitable for use where it is required to synchronize threads over one or more phases of activity. Though Phaser can be used to synchronize a single phase, in that case it acts more like a CyclicBarrier.

    Phaser is reusable (like CyclicBarrier) and more flexible in usage.

    The number of parties registered to synchronize on a phaser may vary over time. Tasks may be registered at any time (using methods register(), bulkRegister(int), or by specifying initial number of parties in the constructor). Tasks may also be optionally deregistered upon any arrival (using arriveAndDeregister()).

    Read more about Phaser here.
  6. What is Exchanger in Java concurrency?

    Exchanger makes it easy for two threads to exchange data between themselves.

    Exchanger provides a synchronization point at which two threads can pair and swap elements. Exchanger waits until two separate threads call its exchange() method. When two threads have called the exchange() method, Exchanger will swap the objects presented by the threads.

    Read more about Exchanger here.
  7. What is Semaphore in Java concurrency?

    The Semaphore class present in java.util.concurrent package is a counting semaphore in which a semaphore, conceptually, maintains a set of permits. Thread that wants to access the shared resource tries to acquire a permit using acquire() method. At that time if the Semaphore's count is greater than zero thread will acquire a permit and Semaphore's count will be decremented by one. If Semaphore's count is zero, when thread calls acquire() method, then the thread will be blocked until a permit is available. When thread is done with the shared resource access, it can call the release() method to release the permit. That results in the Semaphore's count incremented by one.

    Read more about Semaphore here.

Related Topics

  1. Java OOP interview questions
  2. Core Java basics interview questions
  3. Java Exception Handling interview questions
  4. Java Multi-threading interview questions
  5. Java Collections interview questions

Viewing all articles
Browse latest Browse all 862

Trending Articles