Multi-threaded Communication poll sell tickets learn about

Source: Internet
Author: User
Tags print print ticket

Package com.xp.test;

Import java.util.concurrent.locks.Condition;
Import Java.util.concurrent.locks.Lock;
Import Java.util.concurrent.locks.ReentrantLock;

public class Abc_lock {

public static void Main (string[] args) {print print = new print (); New Thread (New Runnable () {//@Override//public void Run () {//for (int i = 1; i <=; i++) {//PR    Int.loopa (i);    }//}//}, "A"). Start (); New Thread (New Runnable () {//@Override//public void Run () {//for (int i = 1; i <=; i++) {//PR    INT.LOOPB (i);    }//}//}, "B"). Start (); New Thread (New Runnable () {//@Override//public void Run () {//for (int i = 1; i <=; i++) {//PR    INT.LOOPC (i);    }//}//}, "C"). Start ();    } int i = 33;        while (i > 0) {i--;            New Thread (New Runnable () {@Override public void run () {Print.loopa ();        }}, "A"). Start ();            New Thread (New Runnable () {@Override public void run () {PRINT.LOOPB ();        }}, "B"). Start (); New Thread (New Runnable() {@Override public void run () {print.loopc ();    }}, "C"). Start (); }}

}

Class Print {
Private lock lock = new Reentrantlock ();
Private Condition Conditiona = Lock.newcondition ();
Private Condition conditionb = Lock.newcondition ();
Private Condition Conditionc = Lock.newcondition ();

int tick = 100;    String mark = "a";p ublic void LoopA () {lock.lock (); try {if (!)            A ". Equals (Mark)) {try {conditiona.await (); } catch (Interruptedexception e) {}} System.out.println (Thread.CurrentThread (). GetName () + "Complete Sale        Ticket, Remaining: "+--tick);        Mark = "B";    Conditionb.signal ();    } finally {Lock.unlock ();    }}public void Loopb () {lock.lock (); try {if (!)            B. Equals (Mark) {try {conditionb.await (); } catch (Interruptedexception e) {}} System.out.println (Thread.CurrentThread (). GetName () + "Complete Sale        Ticket, Remaining: "+--tick);        Mark = "C";    Conditionc.signal ();    } finally {Lock.unlock ();    }}public void Loopc () {lock.lock (); try {if (!)            C ". Equals (Mark) {try {conditionc.await (); } catch (Interruptedexception e) {}} System.out.printlN (Thread.CurrentThread (). GetName () + "complete ticketing, Remaining:" +--tick);        Mark = "a";    Conditiona.signal ();    } finally {Lock.unlock (); }}

}

Multi-threaded Communication poll sell tickets learn about

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.