Java CountDownLatch

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/19

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards

What is CountDownLatch in Java?

A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.

<p>A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.</p>
2
New cards

Which package contains CountDownLatch?

java.util.concurrent

3
New cards

What is the main use of CountDownLatch?

To block a thread until a set of tasks have been completed.

4
New cards

How do you initialize a CountDownLatch?

By specifying the number of times countDown() must be called before threads can proceed, e.g., new CountDownLatch(3).

5
New cards

What does await() do in CountDownLatch?

Causes the current thread to wait until the latch has counted down to zero.

6
New cards

What does countDown() do?

Decrements the count of the latch by one.

7
New cards

What happens if the count is already zero and await() is called?

The thread does not block and proceeds immediately.

8
New cards

Is CountDownLatch reusable?

No, once the count reaches zero, it cannot be reset.

9
New cards

What exception does await() throw?

InterruptedException if the waiting thread is interrupted.

10
New cards

How can you use CountDownLatch for starting multiple threads at the same time?

Use one latch to block all worker threads until the main thread calls countDown(), then they proceed together.

11
New cards

Can await() be timed?

Yes, using await(long timeout, TimeUnit unit).

12
New cards

What happens if the timeout expires in await(long, TimeUnit)?

Returns false and the thread continues execution without waiting for count to reach zero.

13
New cards

How do you check current count of a latch?

Using getCount().

14
New cards

What is a common use case of CountDownLatch?

Waiting for multiple threads to finish their tasks before proceeding (e.g., in test setup or shutdown sequences).

15
New cards

What is the default behavior of await()?

It blocks the thread until the latch count reaches zero or it's interrupted.

16
New cards

Can multiple threads call await() on the same CountDownLatch?

Yes, all waiting threads will proceed once the count reaches zero.

17
New cards

What happens if countDown() is called more times than the initial count?

Extra calls have no effect once count reaches zero.

18
New cards

What is the alternative if you want a reusable latch?

Use CyclicBarrier or Semaphore.

19
New cards

Is CountDownLatch thread-safe?

Yes, all methods are atomic and thread-safe.

20
New cards

Can CountDownLatch be used for testing concurrency code?

Yes, it's often used to coordinate test setup or verify execution order in concurrent unit tests.