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

What are the differences between JAVA Count Down Latch and thread-join ()

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

Share

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

This article mainly introduces "what is the difference between JAVA Count Down Latch and thread-join ()". In daily operation, I believe many people have doubts about the difference between JAVA Count Down Latch and thread-join (). The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what is the difference between JAVA Count Down Latch and thread-join ()"! Next, please follow the editor to study!

First, let's take a look at join. In the current thread, if you call the join method of a thread, the current thread will be blocked until the thread finishes executing. The principle of join is to constantly check whether the thread is alive, and if so, keep the current thread wait until the thread terminates, and the thread's this.notifyAll will be called.

Let's take a look at this application scenario: suppose there are three employees in the company who have a meeting. But A needs to wait until BMageC is ready before it can start, and BMageC needs to be prepared at the same time. Let's first simulate the above scene with join.

Employee.java:

Public class Employee extends Thread {private String employeeName; private long time; public Employee (String employeeName,long time) {this.employeeName = employeeName; this.time = time;} @ Override public void run () {try {System.out.println (employeeName+ "start preparation"); Thread.sleep (time); System.out.println (employeeName+ "ready to complete");} catch (Exception e) {e.printStackTrace ();}

JoinTest.java:

Public class JoinTest {public static void main (String [] args) throws InterruptedException {Employee a = new Employee ("A", 3000); Employee b = new Employee ("B", 3000); Employee c = new Employee ("C", 4000); b.start (); c.start (); b.join (); c.join (); System.out.println ("BForce C ready"); a.start ();}}

The final output is as follows:

C starts preparation B starts preparation B prepares to complete C prepares to complete Brecast C prepares to complete A starts to prepare A prepares to complete

As you can see, An always starts execution after BMagol C is ready to finish.

In CountDownLatch, we mainly use two methods, one is the await () method, the thread calling this method will be blocked, the other is the countDown () method, calling this method will reduce the counter by one. When the value of the counter is 0, the thread blocked by calling the await () method will be awakened to continue execution.

Next, let's simulate it with CountDownLatch.

Employee.java:

Public class Employee extends Thread {private String employeeName; private long time; private CountDownLatch countDownLatch; public Employee (String employeeName,long time, CountDownLatch countDownLatch) {this.employeeName = employeeName; this.time = time; this.countDownLatch = countDownLatch;} @ Override public void run () {try {System.out.println (employeeName+ "start preparation"); Thread.sleep (time); System.out.println (employeeName+ "ready to complete"); countDownLatch.countDown ();} catch (Exception e) {e.printStackTrace ();}

CountDownLatchTest.java:

Public class CountDownLatchTest {public static void main (String [] args) throws InterruptedException {CountDownLatch countDownLatch = new CountDownLatch (2); Employee a = new Employee ("A", 3000 args); Employee b = new Employee ("B", 3000); Employee c = new Employee ("C", 4000 countDownLatch); b.start (); c.start (); countDownLatch.await (); System.out.println ("ready to complete"); a.start ();}}

The output is as follows:

B begins preparation C starts preparation B completes C prepares to complete Brecast C prepares to complete A starts to prepare A prepares to complete

As you can see above, both CountDownLatch and join can simulate the above scenario, so what's the difference between them? At this time, let's imagine another scene and we can see the difference between them.

Suppose that the work of AMagre BPerry C is divided into two phases, and An only needs to wait for BPerry C to complete the first phase of their work.

Let's modify the Employee class:

Public class Employee extends Thread {private String employeeName; private long time; private CountDownLatch countDownLatch; public Employee (String employeeName,long time, CountDownLatch countDownLatch) {this.employeeName = employeeName; this.time = time; this.countDownLatch = countDownLatch;} @ Override public void run () {try {System.out.println (employeeName+ "first phase preparation"); Thread.sleep (time); System.out.println (employeeName+ "first phase preparation"); countDownLatch.countDown () System.out.println (employeeName+ "Phase II preparation"); Thread.sleep (time); System.out.println (employeeName+ "Phase II preparation");} catch (Exception e) {e.printStackTrace ();}

The CountDownLatchTest class does not need to be modified, and the output result is entered below:

B first phase preparation C first phase preparation B first phase preparation B second phase preparation C first phase preparation C second phase preparation B first phase preparation A first phase preparation B second phase preparation A first phase preparation completion A first phase preparation A second phase preparation completion A second phase preparation completion

As can be seen from the results, A begins to execute when the first phase of BMagol C is ready to be completed, and there is no need to wait for the second stage to be completed. In this scenario, it is impossible to use join.

At this point, the study of "what is the difference between JAVA Count Down Latch and thread-join ()" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

*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

Development

Wechat

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

12
Report