9 - What is Count Down Latch in Multithreading? | CountDownLatch | Almighty Java

preview_player
Показать описание
#CountDownLatch #Multithreading #Threads
====================
what is countdown latch?

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

a countdown latch 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. this is a one-shot phenomenon the count cannot be reset.

a countdown latch is a versatile synchronization tool and can be used for a number of purposes. a countdown latch initialized with a count of one serves as a simple on-off latch, or gate all threads invoking await wait at the gate until it is opened by a thread invoking countdown.

a count down latch 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.

a useful property of a countdown latch is that it does not require that threads calling countdown wait for the count to reach zero before proceeding, it simply prevents any thread from proceeding past an await until all threads could pass.

await method of countdown latch class

causes the current thread to wait until the latch has counted down to zero unless the thread is interrupted.

if the current count is zero then this method returns immediately.

if the current count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happen

the count reaches zero due to invocations of the countdown method or some other thread interrupts the current thread

if the current thread has its interrupted status set on entry to this method or is interrupted while waiting then interrupted exception is thrown and the current thread's interrupted status is cleared.

other methods of countdown latch class

countdown - decrements the count of the latch, releasing all waiting threads if the count reaches zero. if the current count is greater than zero then it is decremented. if the new count is zero then all waiting threads are re-enabled for thread scheduling purposes. if the current count equals zero then nothing happens.

get a count - it will return the count like how many more latch is available. so basically use the method for testing and debugging purpose.
Рекомендации по теме
Комментарии
Автор

this is more useful than the other material on YT. Thank you

Somerandomnessvvv