Looping multiple threads out of the 0-99-0-99

Source: Internet
Author: User
Tags static class

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

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.