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