Getting a thread in a blocking queue in a synchronized lock is unfair

Source: Internet
Author: User

The thread that blocks the queue in synchronized is unfair

Test Demo:

ImportJava.text.MessageFormat;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.concurrent.TimeUnit; Public classSleepstate { Public StaticThreadlocal<simpledateformat> ThreadLocal =NewThreadlocal<simpledateformat>() {@OverrideprotectedSimpleDateFormat InitialValue () {return NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss. Sssz ");    }    }; Private Static Final int[] lock =New int[0];  Public Static voidMain (string[] args)throwsinterruptedexception {Thread thread1=NewThread (NewTestsynchronizedtask (lock, + *), "Thread1"); Thread thread2=NewThread (NewTestsynchronizedtask (lock, Ten), "Thread2"); Thread thread3=NewThread (NewTestsynchronizedtask (lock, +), "Thread3");        Thread1.start (); TimeUnit.MILLISECONDS.sleep (1000);        Thread2.start (); TimeUnit.MILLISECONDS.sleep (1000);    Thread3.start (); }}classTestsynchronizedtaskImplementsRunnable {Private Final int[] lock; Private intSleepmilliseconds;  PublicTestsynchronizedtask (int[] Lock,intsleepmilliseconds) {         This. Lock =lock;  This. Sleepmilliseconds =Sleepmilliseconds; }     PublicTestsynchronizedtask (int[] Lock) {         This(Lock, 0); } @Override Public voidrun () {synchronized(lock) {Try{System.out.println (Messageformat.format ("{0} {1} begin", SleepState.threadLocal.get (). Format (NewDate ()), Thread.CurrentThread ()));                TimeUnit.MILLISECONDS.sleep (Sleepmilliseconds); System.out.println (Messageformat.format ("{0} {1} would end", SleepState.threadLocal.get (). Format (NewDate ()), Thread.CurrentThread ())); } Catch(interruptedexception e) {e.printstacktrace (); }        }    }}

2016-05-26 13:31:44.260+0800 thread[thread1,5, main]  begin2016-05-26 13:31:54.260+0800 Thread[thread1, 5, main]  2016-05-26 13:31:54.260+0800 thread[Thread3, 5, main]  begin2016-05-26 13:31:55.260+0800 thread[Thread3, 5, main]  2016-05-26 13:31:55.260+0800 thread[thread2,5 , main]  Begin2016-05-26 13:31:55.276+0800 Thread[thread2,5,main] 'll  end

Getting a thread in a blocking queue in a synchronized lock is unfair

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.