Countdownlatch & Cyclicbarrier & Semaphore

Source: Internet
Author: User
Tags prepare semaphore static class
Countdownlacth

COUNTDOWNLACTH (inverted count latch) What is the use of it? Let's look at the following scene.

We have a small piece to start filming, this small piece requires 5 actors to play, before the show, the director needs the 5 actors are all ready to action,5 an actor to hear the director called Action on the curtain.

Use COUNTDOWNLACTH to complete the above scenario.

public class Countdownlatchdemo {private static final Random Random = new Random ();

    private static final string[] actors ={"Hatano knot Clothes", "Akiho Yoshizawa Step", "Cang jing Empty", "Semaria", "A-ze Lola"};
        Static class Actor implements runnable{private String name;
        Private Countdownlatch readylacth;

        Private Countdownlatch startlacth;
            Public Actor (String name,countdownlatch readylacth,countdownlatch startlacth) {this.name = name;
            THIS.READYLACTH = READYLACTH;
        THIS.STARTLACTH = STARTLACTH;
                @Override public void Run () {try {int readytime = random.nextint (1000);
                SYSTEM.OUT.PRINTLN (name + ": I need" + readytime/100 + "minute time to prepare.");
                Simulation Preparation time Thread.Sleep (readytime);
                SYSTEM.OUT.PRINTLN (name + ": I'm ready!");
                Readylacth.countdown ();
                Waiting for director to Startlacth.await (); System.out.println (NAMe + ": I started acting, Aaaaa,oooooooo,kimoji");
            catch (Interruptedexception e) {e.printstacktrace (); }} public static void Main (string[] args) throws Interruptedexception {//director's LACTH, each actor ready, that LA
        TCH on-1, need to wait for all actors to be ready countdownlatch readylacth = new Countdownlatch (actors.length);

        The actor's lacth needs to wait for the director to call the start, the latch-1 countdownlatch startlacth = new Countdownlatch (1);
        Executorservice Executorservice = Executors.newcachedthreadpool ();
                for (int i=0;i<actors.length;i++) {Actor Actor = new Actor (ACTORS[I],READYLACTH,STARTLACTH);
        Executorservice.execute (actor);
        } readylacth.await ();
        Startlacth.countdown ();
        System.out.println ("Director: Action!");
    Executorservice.shutdown (); }

}
Hatano knot: I need 5 minutes to prepare.
Akiho Yoshizawa Step: I need 2 minutes to prepare.
Cang Jing Empty: I need 3 minutes to prepare.
Semaria: I need 6 minutes to prepare.
Long jersey: I need 4 minutes to prepare.
Akiho Yoshizawa Step: I'm ready!
Cang Jing Empty: I am ready!
Laura: I'm ready!
Hatano knot: I'm ready!
Semaria: I'm ready!
Director: action!
Cang Jing Empty: I began to perform, Aaaaa,oooooooo,kimoji
Semaria: I began to perform, Aaaaa,oooooooo,kimoji
Akiho Yoshizawa Step: I began to perform, aaaaa,oooooooo, Kimoji
Hatano: I started acting, Aaaaa,oooooooo,kimoji.
Lola: I started acting, Aaaaa,oooooooo,kimoji
Cyclicbarrier

Now, we're going to take two scenes and divide the actors into two groups (the same number), and the director is going to shoot the two scenes.

You can also use COUNTDOWNLACTH to complete this requirement and reset countdownlacth after the first scene has been shot. Use Cyclicbarrier can be reused.

Use Cyclicbarrier to complete as needed.

public class Cyclicbarrierdemo {private static final Random Random = new Random ();
    private static final string[] ACTORS1 ={"Hatano knot Clothes", "Akiho Yoshizawa Step", "Cang jing Empty"};

    private static final string[] ACTORS2 ={"Semaria", "" "" "", "" Azomi "};
        Static class Actor implements runnable{private String name;

        Private Cyclicbarrier barrier;
            Public Actor (String name,cyclicbarrier barrier) {this.name = name;
        This.barrier = barrier;
                @Override public void Run () {try {int readytime = random.nextint (1000);
                SYSTEM.OUT.PRINTLN (name + ": I need" + readytime/100 + "minute time to prepare.");
                Simulation Preparation time Thread.Sleep (readytime);
                SYSTEM.OUT.PRINTLN (name + ": I'm ready!");
                Waiting for director to Barrier.await ();
            SYSTEM.OUT.PRINTLN (name + ": I started acting, Aaaaa,oooooooo,kimoji"); catch (Interruptedexception e) {E.printstackTrace ();
            catch (Brokenbarrierexception e) {e.printstacktrace (); }} public static void Main (string[] args) throws Interruptedexception {Cyclicbarrier barrier = new Cyclicbarrier (actors1.length, New Runnable () {public void run () {System.out.println ("guide
        Act: action! ");
            for (int i = 0; i < actors1.length i++) {thread t = new Thread (new Actor (Actors1[i],barrier));
        T.start ();
        } thread.sleep (10000);
        System.out.println ("============== change Scene ==========================");
            for (int i = 0; i < actors2.length i++) {thread t = new Thread (new Actor (Actors2[i],barrier));
        T.start (); }
    }
}
Hatano knot: I need 4 minutes to prepare.
Akiho Yoshizawa Step: I need 0 minutes to prepare.
Cang Jing Empty: I need 6 minutes to prepare.
Akiho Yoshizawa Step: I'm ready!
Hatano knot: I'm ready!
Cang Jing Empty: I am ready!
Director: action!
Cang Jing Empty: I began to perform, Aaaaa,oooooooo,kimoji
Akiho Yoshizawa Step: I began to perform, Aaaaa,oooooooo,kimoji
hatano knot clothes: I began to perform, Aaaaa,oooooooo,kimoji
============== Replacement Scene ==========================
Semaria: I need 6 minutes to prepare.
Long jersey: I need 1 minutes to prepare.
Azomi: I need 8 minutes to prepare.
Laura: I'm ready!
Semaria: I'm ready!
Azomi: I'm ready!
Director: action!
Azomi: I started acting, Aaaaa,oooooooo,kimoji.
Lola: I started acting, Aaaaa,oooooooo,kimoji
Semaria: I started acting, aaaaa,oooooooo, Kimoji
Semaphore

The recent actress is a bit lazy, love playing big cards, preparation for a long time is not good, delay. Are big names, and dare not offend, the Director Group decided, the first ready to shoot first, the back to the order in line, and so on the front of the final shot, not to delay the time.

It's more convenient to use semaphore at this time.

public class Semaphoredemo {private static final Random Random = new Random ();

    private static final string[] actors ={"Hatano knot Clothes", "Akiho Yoshizawa Step", "Cang jing Empty", "Semaria", "the" "", "" Azomi "};
        Static class Actor implements runnable{private String name;

        Private semaphore semaphore;
            Public Actor (String name,semaphore semaphore) {this.name = name;
        This.semaphore = semaphore;
                @Override public void Run () {try {int readytime = random.nextint (1000);
                SYSTEM.OUT.PRINTLN (name + ": I need" + readytime/100 + "minute time to prepare.");
                Simulation Preparation time Thread.Sleep (readytime);
                if (Semaphore.availablepermits () >0) {System.out.println (name + ": I'm ready!");
                }else{System.out.println (name + ": I'm ready! Ah, late");
                } semaphore.acquire (); SYSTEM.OUT.PRINTLN (name + ": I started acting, AAAAA,Oooooooo,kimoji ");
                Thread.Sleep (1000);
                SYSTEM.OUT.PRINTLN (name + ": Finished!");
            Semaphore.release ();
            catch (Interruptedexception e) {e.printstacktrace (); }} public static void Main (string[] args) {Executorservice executorservice= Executors.newcach
        Edthreadpool ();
        Final semaphore semaphore = new semaphore (3);
            for (int i=0;i<actors.length;i++) {Actor Actor = new Actor (Actors[i],semaphore);
        Executorservice.execute (actor);
    } executorservice.shutdown (); }
}
 Akiho Yoshizawa Step: I need 6 minutes to prepare. Hatano knot: I need 9 minutes to prepare. Cang Jing Empty: I need 1 minutes to prepare. Semaria: I need 4 minutes to prepare. Long ze: I need 5 minutes to prepare.
Azomi: I need 8 minutes to prepare.
Cang Jing Empty: I am ready!
Cang Jing Empty: I began to perform, Aaaaa,oooooooo,kimoji Semaria: I am ready!
Semaria: I'm starting the show, Aaaaa,oooooooo,kimoji. Lola: I'm ready!
Aaaaa,oooooooo,kimoji: I started acting, Akiho Yoshizawa step: I'm ready! Ah, it's late. Azomi: I'm ready! Ah, it's late. Hatano: I'm ready! Ah, it's late. Cang Jing empty: Finished!
Akiho Yoshizawa Step: I began to perform, Aaaaa,oooooooo,kimoji Semaria: Finished!
Azomi: I'm starting the show, Aaaaa,oooooooo,kimoji. Lola: Finish!
Hatano knot clothes: I began to perform, Aaaaa,oooooooo,kimoji Akiho Yoshizawa step: Finished!
Azomi: Finish the play! Hatano knot clothes: finished! 

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.