Directly on the code, after I also have a question, about the object of Notifyall or notify or single or Singleall, wake-up thread is sequential wake up, I here are sequential output
On the issue of fair and unfair lock-up? Is it fair to awaken??? And no big guy to help answer!
1 Import java.util.concurrent.LinkedBlockingQueue; 2 Import Java.util.concurrent.ThreadPoolExecutor; 3 Import Java.util.concurrent.TimeUnit; 4 Import java.util.concurrent.locks.Condition; 5 Import Java.util.concurrent.locks.Lock; 6 Import Java.util.concurrent.locks.ReentrantLock; 7 8/** 9 * Use the Lock object to enable multiple threads to loop output 0-99-0-9910 */11 public class Aandbfortothreadlock {$ private static Lock lock=new R Eentrantlock (); private static Condition single=lock.newcondition (); Static class A implements RUNNABLE{15 private int i=0;16 Public A (int. i) {this.i=i;18}19 @Override20 public void run () {try {lock.lock (); + (true) {Syst Em.out.println (i); single.await (); single.signal (); 27}28 }catch (Exception e) {e.printstacktrace ();}finally {31 Lock.unlock ();}33}34}35 Static class B implements RUNNABLE{36 @Override37 public void Run () {$ try {lock.lock (); + (true) {41 System.out.println ("Single.signalall"); single.await (); 44 }45}catch (Exception e) {e.printstacktrace ();}finally {48 Lock.unlock ();}50}51}52: public static void Main (string[] args) throws Interrup tedexception {threadpoolexecutor threadpoolexecutor=new threadpoolexecutor (100,100,0l, TimeUnit.MILLISECONDS,55 New Linkedblockingqueue<runnable> ()), i=0;i<99;i++ for (int) {ThreadPool Executor.execute (New A (i)), Thread.Sleep (3l),}60 thread.sleep (10000l), THREADPO Olexecutor.execute (New B()); 62}63 64}
Output:
......
Looping multiple threads out of the 0-99-0-99