Example to explain the blocking of Java concurrent programming _java

Source: Internet
Author: User

The latch is the equivalent of a door, which is closed until it reaches the end state, and no thread can pass through it, and when the end state is reached, the door opens and allows all threads to pass. It can cause one or more threads to wait for a set of events to occur. The lockout state includes a counter initialized to a formal, positive number indicating the amount of events that need to wait. The countdown method decrements the counter, indicating that an event has occurred, while the await method waits for the counter to reach 0, indicating that the waiting event has occurred. Countdownlatch emphasizes that one thread (or more) needs to wait for another n thread to do something before continuing.

Scenario Application:
10 athletes prepare for the race, they wait for the referee command to start running at the same time, when the last person through the end, the game is over. 10 motions are equivalent to 10 threads, where the key is to control 10 threads running at the same time, and how to determine the last thread to reach the end point. You can use 2 latch, the first latch to control 10 threads waiting for the referee's command, the second lockout control game is over.

Import Java.util.concurrent.CountDownLatch;
 Class Aworker implements Runnable {private int num;
 Private Countdownlatch begin;
 
 Private Countdownlatch end;
 public aworker (int num, final countdownlatch begin, final Countdownlatch end) {this.num = num;
 This.begin = begin;
 This.end = end; @Override public void Run () {//TODO auto-generated Method stub try {System.out.println (num + ' th people is re
  Ady ");  Begin.await ();
 Ready to catch (Interruptedexception e) {e.printstacktrace ();  finally {End.countdown ();
 Counter minus one, to reach the end point System.out.println (num + "th people arrive");
 }} public class Race {public static void main (string[] args) {int num = 10;
 Countdownlatch begin = New Countdownlatch (1);
 
 Countdownlatch end = new Countdownlatch (num);
 for (int i = 1; I <= num i++) {New Thread (the new Aworker (I, Begin, end)). Start ();
 try {thread.sleep (long) (Math.random () * 5000)); catch (Interruptedexception E1) {//TODO auto-generated CATCH block E1.printstacktrace ();
 } System.out.println ("Judge Say:run!"); Begin.countdown ();
 Referee command started running long starttime = System.nanotime (); try {end.await ()//wait to end} catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ()
 ;
  Finally {long endtime = System.nanotime ();
  SYSTEM.OUT.PRINTLN ("Judge Say:all arrived!");
 System.out.println ("Spend time:" + (Endtime-starttime));

 }
 }
}

Output

1th people is ready
2th people are ready
4th people is ready
6th people are ready
3th people is ready
10 Th people is ready
8th people are ready
5th people is ready
7th people are ready
9th people is ready
ju Dge Say:run!
1th people arrive
4th people arrive
10th people arrive
5th people arrive
2th people arrive
Judge Say:all arrived!
9th people arrive
7th people arrive
8th people arrive
3th people arrive
6th people arrive
spend time:970933

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.