Multi-thread countdown timer countdownlatch and loop fence Cyclicbarrier

Source: Internet
Author: User

1. Countdown Timer Countdownlatch
Countdownlatch is a multi-line program-based tool class. Typically used to control thread waits, it allows a thread to wait until the timing is over before it starts executing.
constructor function:
public Countdownlatch (int count)//count count

例如:在主线程中启动10个子线程去数据库中获取分页数据,需要等到所有线程数据都返回之后统一做统计处理

public class Countdownlatchdemo implements runnable{
private static final Countdownlatch end = new Countdownlatch (10);
@Override
public void Run () {
SYSTEM.OUT.PRINTLN ("Thread Name:" + Thread.CurrentThread (). GetName ());
Data Service Processing
End.countdown ();//Count minus 1
}

public static void main(String[] args) {    CountDownLatchDemo countDownLatchDemo = new CountDownLatchDemo();    System.out.println("计数开始");    for (int i = 0; i < 10; i++) {        new Thread(countDownLatchDemo).start();    }    try {        end.await();        System.out.println("计数结束");    } catch (InterruptedException e) {        e.printStackTrace();    }}

}

上述代码使用CountDownLatch的await()方法,要求主线程等待所有10个计数任务全部完成后,主线程才开始继续执行可以通过getCount()方法获取当前的计数个数,当计数结束时count为0,且不会被重置

2. Circular Fence Cyclicbarrier
Cyclicbarrier is also a tool for multithreading concurrency control. More powerful than Countdownlatch,cyclicbarrier features, mainly in:
1) Construction Method:
public Cyclicbarrier (int parties, Runnable barrieraction)
Cyclicbarrier's construction method can be passed to a runnable barrieraction, which can be used to do a specific thing after the thread is assembled.
2) Cyclicbarrier can be reused, when a batch of thread parties is, the counter will be zeroed, restart counting

public class Cyclicbarrierdemo {

Class Soldier implements runnable{private Cyclicbarrier cyclicbarrier;    Public Soldier (Cyclicbarrier cyclicbarrier) {this.cyclicbarrier = Cyclicbarrier;        } @Override public void Run () {System.out.println ("soldier" + Thread.CurrentThread (). GetId () + "report");            try {//wait for all soldiers to go to Qi cyclicbarrier.await ();            Perform task doWork ();        Cyclicbarrier.await ();        } catch (Interruptedexception e) {e.printstacktrace ();        } catch (Brokenbarrierexception e) {e.printstacktrace ();        }} private void DoWork () throws Interruptedexception {thread.sleep (1000);    System.out.println ("Soldier" + Thread.CurrentThread (). GetId () + "task Complete");    }}class Commond implements runnable{@Override public void Run () {System.out.println ("End of Task");    }}public static void Main (string[] args) {Cyclicbarrierdemo Cyclicbarrierdemo = new Cyclicbarrierdemo (); Cyclicbarrier Cyclicbarrier = New Cyclicbarrier (5, Cyclicbarrierdemo.new Commond ());    for (int i = 0; i <; i++) {New Thread (cyclicbarrierdemo.new Soldier (Cyclicbarrier)). Start (); }}

}

Execution Result:

As you can see, the thread is divided into 5 groups of

Multi-thread countdown timer countdownlatch and loop fence Cyclicbarrier

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.