Controls only 3 threads are completed at the same time before the thread can continue execution

Source: Internet
Author: User

Import Java.util.concurrent.cyclicbarrier;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;public class Cyclicbarriertest {public static void main (string[] args) {Executorservice Service = Executors.newcachedthreadpool (); Final Cyclicbarrier cb = new Cyclicbarrier (3); for (int i=0;i<3;i++) {Runnable Runnable = new Runnable () {public void run () {try {Thread.Sleep ((long) (Math.random () *10 000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "upcoming meeting place 1, currently available" + (cb.getnumberwaiting () +1) + "One has arrived," + ( Cb.getnumberwaiting () ==2? " All of them, keep going ":" Waiting "); Cb.await (); Thread.Sleep ((Long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "upcoming meeting place 2, currently available" + (cb.getnumberwaiting () +1) + "One has arrived," + ( Cb.getnumberwaiting () ==2? " All of them, keep going ":" Waiting "); Cb.await (); Thread.Sleep ((Long) (Math.random () *10000)); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "upcoming meeting place 3, currently available" + (cb.getnumberwaiting () + 1) + "One has arrived," + ( CB.getnumberwaiting () ==2? " All of them, keep going ":" Waiting "); Cb.await (); } catch (Exception e) {e.printstacktrace ();}} }; Service.execute (runnable); } service.shutdown (); }}

Control only 3 threads are finished at the same time before the thread can continue to execute

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.