The Countdownlatch of Java multithreaded programming

Source: Internet
Author: User

The main methods of Java.util.concurrent.CountDownLatch in this class are:

1.countDown (), decrements the count of the latch, releasing all waiting threads if the count reaches zero.

2.await (), causes the current thread to wait until the latch have counted down to zero and unless the thread is interrupted.

Runner.javapackage Com.cc;import Java.util.concurrent.countdownlatch;public class Runner implements runnable{ Private Countdownlatch latch;private int index;public Runner (int index) {this.index = index;} Public Runner (countdownlatch latch, int index) {this.latch = Latch;this.index = index;} @Overridepublic void Run () {//TODO auto-generated method stubtry {thread.sleep); Latch.countdown (); SYSTEM.OUT.PRINTLN ("Latch countdown:" +this.index);} catch (Interruptedexception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} try {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} SYSTEM.OUT.PRINTLN ("Latch countdown:" +this.index+ "End");} Public Countdownlatch Getlatch () {return latch;} public void Setlatch (Countdownlatch latch) {this.latch = latch;}} Test.javapackage Com.cc;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.executors;import Java.util.concurrent.executorservice;public class Test {public staticvoid Main (string[] args) {//TODO auto-generated method Stubcountdownlatch latch = new Countdownlatch (10); Executorservice Execserv = executors.newfixedthreadpool (int i = 0; i <; i++) {Execserv.submit (new Runner (LA TCH, i));} try {thread.sleep (1); latch.await (); SYSTEM.OUT.PRINTLN ("Latch await through Pass");} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println ("--end--");}}

Runner.javapackage Com.cc;import Java.util.concurrent.countdownlatch;public class Runner implements runnable{ Private Countdownlatch latch;private countdownlatch backlatch;private int index;public Runner (int index) {This.index = Index;} Public Runner (Countdownlatch latch, countdownlatch backlatch, int index) {this.latch = Latch;this.backlatch = Backlatch; This.index = index;} @Overridepublic void Run () {//TODO auto-generated method stubtry {System.out.println ("Latch countdown:" +this.index); Latch.countdown (); System.out.println ("latch:" +this.index); backlatch.await ();} catch (Interruptedexception E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} SYSTEM.OUT.PRINTLN ("Latch countdown:" +this.index+ "End");} Public Countdownlatch Getlatch () {return latch;} public void Setlatch (Countdownlatch latch) {this.latch = latch;} Public Countdownlatch Getbacklatch () {return backlatch;} public void Setbacklatch (Countdownlatch backlatch) {this.backlatch = Backlatch;}} Test.javapackage Com.cc;import JAva.util.concurrent.countdownlatch;import Java.util.concurrent.executors;import Java.util.concurrent.executorservice;public class Test {public static void main (string[] args) {//TODO auto-generated Me Thod Stubcountdownlatch latch = new Countdownlatch (10); Countdownlatch Backlatch = new Countdownlatch (1); Executorservice Execserv = executors.newfixedthreadpool (int i = 0; i <; i++) {Execserv.submit (new Runner (LA TCH, Backlatch,i));} try {latch.await (); SYSTEM.OUT.PRINTLN ("Latch await through pass"); Backlatch.countdown ();} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} System.out.println ("--end--");}}

  

The Countdownlatch of Java multithreaded programming

Related Article

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.