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 Latch instead of wait notify for notification

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

Share

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

This article will explain in detail how to use Latch instead of wait notify to notify, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

/ * once the interview question: (Taobao? ) * implement a container and provide two methods: add,size * write two threads, thread 1 adds 10 elements to the container, and thread 2 monitors the number of elements. When the number reaches 5, thread 2 gives a prompt and ends * * after adding volatile to lists, T2 can be notified. However, the dead loop of T2 threads is a waste of cpu. What should I do if I don't need a dead loop? * * using wait and notify to do this, wait will release the lock, but notify will not release the lock * it is important to note that in this method, T2 must be executed first, that is, T2 listens first before you can * * read the following program and analyze the output * you can read that T2 exits when the output is not size=5 But it is only when T1 ends that T2 receives the notice and quits * think about why? * * after notify, T1 must release the lock, and after T2 exits, it must also notify to notify T1 to continue to execute * the whole communication process is cumbersome * * use Latch (latch) instead of wait notify to notify * the advantage is that the communication mode is simple, at the same time, you can also specify waiting time * use await and countdown methods instead of wait and notify * CountDownLatch does not involve locking When the value of count is 00:00, the current thread continues to run * when synchronization is not involved, but thread communication is involved, using synchronized + wait/notify is too heavy * at this point, you should consider countdownlatch/cyclicbarrier/semaphore * @ author mashibing * / package yxxy.c_019 Import java.util.ArrayList;import java.util.List;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;public class MyContainer5 {/ / add volatile so that T2 can be notified volatile List lists = new ArrayList (); public void add (Object o) {lists.add (o);} public int size () {return lists.size () } public static void main (String [] args) {MyContainer5 c = new MyContainer5 (); / / the latch (when this 1 becomes 0, the latch opens) CountDownLatch latch = new CountDownLatch (1); new Thread (()-> {System.out.println ("T2 start") If (c.size ()! = 5) {try {/ / latch waiting does not require locking any object latch.await () / / you can also specify the waiting time / / latch.await (5000, TimeUnit.MILLISECONDS) } catch (InterruptedException e) {e.printStackTrace ();}} System.out.println ("T2 end");}, "T2") .start () Try {TimeUnit.SECONDS.sleep (1);} catch (InterruptedException E1) {e1.printStackTrace ();} new Thread (()-> {System.out.println ("T1 startup") For (int I = 0; I < 10; iTunes +) {c.add (new Object ()); System.out.println ("add" + I) If (c.size () = = 5) {/ / Open the latch so that T2 can execute latch.countDown () } try {TimeUnit.SECONDS.sleep (1);} catch (InterruptedException e) {e.printStackTrace () }}, "T1"). Start ();}} on how to use Latch instead of wait notify to notify here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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: 220

*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