Home  >  Article  >  Java  >  Detailed explanation of multi-thread synchronization class CountDownLatch

Detailed explanation of multi-thread synchronization class CountDownLatch

Y2J
Y2JOriginal
2017-05-11 09:34:031869browse

This article mainly introduces the relevant knowledge of the multi-thread synchronization class CountDownLatch in Java, which has a good reference value. Let's take a look with the editor below

In multi-threaded development, we often encounter operations that we hope will happen after a group of threads are completed. Java provides a multi-thread synchronization auxiliary class that can complete this type of operation. Requirements: Common methods in the

class:

##Construction method:

CountDownLatch(int count) The parameter count is a counter, generally assigned with the number of threads to be executed.

long getCount(): Get the current counter value.

void countDown(): When the counter value is greater than zero, the method is called and the counter value is reduced by 1. When the counter reaches zero, all threads are released.

void await(): Call this method to block the current main thread until the counter decreases to zero.

Code example:

Thread class:

import java.util.concurrent.CountDownLatch;
public class TestThread extends Thread{
CountDownLatch cd;
String threadName;
public TestThread(CountDownLatch cd,String threadName){
 this.cd=cd;
 this.threadName=threadName;

}
@Override
public void run() {
 System.out.println(threadName+" start working...");
 dowork();
 System.out.println(threadName+" end working and exit...");
 cd.countDown();//告诉同步类完成一个线程操作完成

}
private void dowork(){
 try {
 Thread.sleep(2000);
 System.out.println(threadName+" is working...");
 } catch (InterruptedException e) {
 // TODO Auto-generated catch block
 e.printStackTrace();
 }

}

}

Test class:

import java.util.concurrent.CountDownLatch;
public class TsetCountDownLatch {

 public static void main(String[] args) {
 try {
  CountDownLatch cd = new CountDownLatch(3);// 表示一共有三个线程
  TestThread thread1 = new TestThread(cd, "thread1");
  TestThread thread2 = new TestThread(cd, "thread2");
  TestThread thread3 = new TestThread(cd, "thread3");
  thread1.start();
  thread2.start();
  thread3.start();
  cd.await();//等待所有线程完成
  System.out.println("All Thread finishd");
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 }
}

Output result:

 thread1 start working...
 thread2 start working...
 thread3 start working...
 thread2 is working...
 thread2 end working and exit...
 thread1 is working...
 thread3 is working...
 thread3 end working and exit...
 thread1 end working and exit...
 All Thread finishd

【Related recommendations】

1.

java free video tutorial

2.

Geek Academy Java video tutorial

3.

Alibaba Java Development Manual

The above is the detailed content of Detailed explanation of multi-thread synchronization class CountDownLatch. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn