Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use CountDownLatch to complete LeetCode1114

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use CountDownLatch to complete LeetCode1114", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use CountDownLatch to complete LeetCode1114.

1) the count value set after CountDownLatch initialization cannot be restored after it is reduced to 0, and Semaphore can recover the number of signals / permits through release, so the problem range that CountDownLatch can solve is less than Semaphore. In the code that uses the operation of semaphore.release, there is basically no way to replace Semaphore with CountDownLatch. CountDownLatch is like a disposable door bolt to the door. After the bolt is opened, the door can only be 'open'. We do not have the ability to close the door again. While Semaphore is more like a signal light, we can give different 'red' and 'release' signals according to our needs, so the applicable scenarios can be more complex.

So for LeetCode multithreaded exercises, only 1114 can be done with CountDownLatch.

2) in addition, in terms of allowing and prohibiting this direction, Semaphore is a signal that threads can run when the number of permits is greater than 0 (semapher.acquire does not block), and for CoundDownLatch, threads can run when the count is equal to 0 (countDownLatch.await does not block).

Complete LeetCode 1114 with CountDownLatch:

Public class FooByCDL {

Private CountDownLatch cdl2 = new CountDownLatch (1); private CountDownLatch cdl3 = new CountDownLatch (1); public FooByCDL () {}

Public void first (Runnable printFirst) throws InterruptedException {printFirst.run (); cdl2.countDown ();}

Public void second (Runnable printSecond) throws InterruptedException {cdl2.await (); printSecond.run (); cdl3.countDown ();}

Public void third (Runnable printThird) throws InterruptedException {cdl3.await (); printThird.run ();}

Public static void main (String [] args) {FooByCDL foo = new FooByCDL ()

New Thread (()-> {try {foo.third (()-> System.out.print ("three"));} catch (InterruptedException ie) {ie.printStackTrace ();}}) .start ()

New Thread (()-> {try {foo.second (()-> System.out.print ("two"));} catch (InterruptedException ie) {ie.printStackTrace ();}}) .start ()

New Thread (()-> {try {foo.first (()-> System.out.print ("one"));} catch (InterruptedException ie) {ie.printStackTrace ();}}). Start ();}} so far, I believe you have a deeper understanding of "how to use CountDownLatch to complete LeetCode1114". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report