Grokking The Java Developer Interview: More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview


Question 91: What is Count down latch in Java?



Download 7,31 Mb.
Pdf ko'rish
bet68/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   64   65   66   67   68   69   70   71   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Question 91: What is Count down latch in Java?
Answer: CountDownLatch is used in requirements where you want
one or more tasks to wait for some other tasks to finish before it
starts its own execution. One example can be a server which is
dependent on some services to be up and running before it can start
processing requests.
How to use: When we create an object of CountDownLatch, then we
specify the number of threads that it should wait for, then waiting
thread calls the countDownlatch.await() method and until all the
specified thread calls countDownLatch.countDown() method, the
waiting thread will not start its execution.
For example, there are 3 services which a server is dependent on,
before the server accepts any request, these services should be up
and running. We will create a CountDownLatch by specifying 3
threads that the main thread should wait for, then main thread will
call await() method means it will wait for all 3 threads. Once the
threads are complete they will call countdown() method, decreasing
the count by 1. The main thread will start its execution only when
count reaches to zero.


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
91\color1.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
91\color2.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
91\output.png


CountDownLatch can also be used to start multiple threads at the
same time, you can create a CountDownLatch of size 1, make all the
other threads wait by calling countDownLatch.await(), then a single
call to countDownLatch.countDown() method will resume execution
for all the waiting threads at the same time.
CountDownLatch cannot be reused once the count reaches to zero,
therefore in those scenarios, CyclicBarrier is used.

Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   64   65   66   67   68   69   70   71   ...   89




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish