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 Java concurrent programming Synchronizer CountDownLatch

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge about how to use the Java concurrent programming synchronizer CountDownLatch. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

CountDownLatch

In daily development, we often encounter situations where multiple threads need to be opened in the main thread to execute tasks in parallel, and the main thread needs to wait for all the child threads to finish execution before summarizing. Before the advent of CountDownLatch, the threaded join () method was used to achieve this, but the join method was not flexible enough to meet the needs of different scenarios, so the JDK development team provided the CountDownLatch class, and the example we introduced earlier would be more elegant with CoumtDownLatch.

The code for using CountDownLatch is as follows:

Package LockSupportTest;import java.util.concurrent.CountDownLatch;public class JoinCountDownLatch {private static volatile CountDownLatch countDownLatch = new CountDownLatch (2); public static void main (String [] args) throws InterruptedException {Thread threadOne = new Thread (new Runnable () {@ Override public void run () {try {Thread.sleep (1000); System.out.println ("child threadOne over!") } catch (InterruptedException e) {e.printStackTrace ();} finally {countDownLatch.countDown ();}) Thread threadTwo = new Thread (new Runnable () {@ Override public void run () {try {Thread.sleep (1000); System.out.println ("child threadOne over!");} catch (InterruptedException e) {e.printStackTrace () } finally {countDownLatch.countDown ();}); threadOne.start (); threadTwo.start (); System.out.println ("wait all child thread overloading!"); countDownLatch.await (); System.out.println ("all child thread over!");}}

In the code above, an instance of CountDownLatch is created, and because there are two child threads, the constructor passes a parameter of 2. The main thread is blocked after calling the countDownLatch.await () method. After the child thread finishes execution, call the countDownLatch.countDown () method to subtract the counter inside the countDownLatch by 1. After all the child threads finish execution and call the countDown () method, the counter will become 0, and then the await () method of the main thread will return. In fact, the above code is not elegant enough, in project practice generally avoid direct manipulation of threads, but use the ExceutorService thread pool to manage, the parameter passed when using ExcsuIwsnise is Runable or Callable object, at this time you have no way to directly call the join () method of these threads, you need to choose to use CountDownLatch.

Modify the above code to:

Package LockSupportTest;import java.util.concurrent.CountDownLatch;import java.util.concurrent.Executor;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class JoinCountDownLatch3 {private static volatile CountDownLatch countDownLatch = new CountDownLatch (2); public static void main (String [] args) throws InterruptedException {ExecutorService executorService = Executors.newFixedThreadPool (2) ExecutorService.submit (new Runnable () {@ Override public void run () {try {Thread.sleep (1000); System.out.println ("child threadOne over!");} catch (InterruptedException e) {e.printStackTrace () } finally {countDownLatch.countDown ();}); executorService.submit (new Runnable () {@ Override public void run () {try {Thread.sleep (1000)) System.out.println ("child threadTwo over!");} catch (InterruptedException e) {e.printStackTrace ();} finally {countDownLatch.countDown ();}); System.out.println ("wait all child thread overloading!") CountDownLatch.await (); System.out.println ("all child thread over!"); executorService.shutdown ();}}

These are all the contents of the article "how to use the Java concurrent programming Synchronizer CountDownLatch". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report