Java multi-thread cyclicbarrier simple usage

Source: Internet
Author: User
public class Cyclicbarriertest {public static void main (string[] args) {Executorservice service = Executors.newcache
		Dthreadpool ();
		Final Cyclicbarrier cb = new Cyclicbarrier (3); for (int i=0;i<3;i++) {Runnable Runnable = new Runnable () {public void run () {try {thread.sleep (lon	
						g) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "is about to arrive at the rendezvous point 1, there is currently" + (Cb.getnumberwaiting () +1) + "The one has been to DA, "+ (cb.getnumberwaiting () ==2?"						
						All here, keep walking ": Waiting");
						
						Cb.await ();	
						Thread.Sleep ((Long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "is about to arrive at the rendezvous point 2, there is currently" + (Cb.getnumberwaiting () +1) + "The one has been to DA, "+ (cb.getnumberwaiting () ==2?"
						All here, keep walking ": Waiting");	
						Cb.await ();	
						Thread.Sleep ((Long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "is about to arrive at the rendezvous point 3, currently has" + (cb.getnumberwaiting () + 1) + "A has been Upon arrival, "+ (Cb.getnumbeRwaiting () ==2? "						
						All here, keep walking ": Waiting");						
					Cb.await ();
					catch (Exception e) {e.printstacktrace ();
			}				
				}
			};
		Service.execute (runnable);
	} service.shutdown ();
 }
}

The results of the operation are as follows:

The thread pool-1-thread-3 is about to arrive at the rendezvous point 1, currently 1 have arrived and are waiting for
The thread pool-1-thread-1 is about to arrive at the rendezvous point 1, currently 2 have arrived and are waiting for
Thread pool-1-thread-2 is about to arrive at the rendezvous point 1, there are now 3 already arrived, are all here, keep going.
The thread pool-1-thread-3 is about to arrive at the Rendezvous point 2, currently 1 have arrived and are waiting for
The thread pool-1-thread-1 is about to arrive at the Rendezvous point 2, currently 2 have arrived and are waiting for
Thread pool-1-thread-2 is about to arrive at the rendezvous point 2, there are now 3 already arrived, are all here, keep going.
The thread pool-1-thread-1 is about to arrive at the rendezvous point 3, currently 1 have arrived and are waiting for
The thread pool-1-thread-3 is about to arrive at the rendezvous point 3, currently 2 have arrived and are waiting for
Thread pool-1-thread-2 is about to arrive at the rendezvous point 3, there are now 3 already arrived, are all here, keep going.


Organize from tutorial

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.