Multi-threaded Learning fair lock and unfair lock

Source: Internet
Author: User

Fair and unfair Lock: Lock lock is divided into fair lock and unfair lock, fair lock means that the order of acquiring locks in the field is allocated according to the order of line Cheng,

First-come first-served FIFO first-out order. The non-fair lock is a preemption mechanism for acquiring locks, which is a random lock, and a fair lock is not the same as the first

You don't have to get the lock first, which can cause some threads to never get a lock.

  

First, verify the Fair Lock: Create the service method, lock with lock.

  

public class Service {private Reentrantlock Lock;public service (Boolean Isfair) {super (); System.out.println (Isfair); lock=new Reentrantlock (Isfair);} public void Servicemethod (String str) {try {lock.lock (); ("a". Equals (str)) {//The thread to be started is all blocked in this try {thread.sleep (2000);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}} System.out.println ("Threadname=" +thread.currentthread (). GetName () + "acquired lock");} finally {Lock.unlock ();}}}

Then run the validation:

public class Run {public static void main (string[] args) throws Interruptedexception {fair (true);//fair (false);} public static void Fair (Boolean  Isfair) throws interruptedexception{final Service Service=new service (Isfair); new Thread (new Runnable () {public void run () {System.out.println ("the first thread, which blocks the role of a later thread"), Service.servicemethod ("a");}}). Start (); Thread.Sleep (500); Runnable runnable=new  Runnable () {public void run () {System.out.println ("thread" +thread.currentthread (). GetName () + " Run "); Service.servicemethod (" B ");}; Thread [] threads=new thread[10];for (int i = 0; i < threads.length; i++) {threads[i]=new thread (runnable);} for (int i = 0; i < threads.length; i++) {Threads[i].start ();}}}

Fair Lock Consoles:

True the first thread, which is blocked by the thread Thread-1 running thread Thread-2 running thread Thread-3 running thread Thread-5 running thread Thread-4 running thread Thread-6 running thread Thread-7 Line up thread Thread-8 run thread Thread-9 run thread Thread-10 run threadname= Thread-0 get lock threadname= Thread-1 get lock threadname= Thread-2 obtained The lock Threadname= Thread-3 obtained the lock threadname= Thread-4 obtained the lock threadname= Thread-5 obtained the lock Threadname= Thread-7 acquired the lock threadname= Thread-6 acquired the lock Threadname= Thread-8 acquired the lock threadname= Thread-9 obtained the lock Threadname= Thread-10 acquired the lock

It can be found that the console is ordered, where 0 threads are meant to be easy to watch, intentionally increased, and all threads are blocked here.

Non-fair lock console:

False the first thread, which is blocked by the thread Thread-1 running thread Thread-3 running thread Thread-2 running thread Thread-4 running thread Thread-5 running thread Thread-10 running thread Thread-9 Run thread Thread-8 run thread Thread-7 run thread Thread-6 run threadname= Thread-0 get lock threadname= Thread-2 get lock threadname= Thread-3 get The lock Threadname= Thread-1 obtained the lock threadname= Thread-4 obtained the lock threadname= Thread-5 obtained the lock threadname= Thread-10 obtained the lock threadname= Thread-9 acquired the lock Threadname= Thread-8 acquired the lock threadname= Thread-6 obtained the lock Threadname= Thread-7 acquired the lock

It can be found that advanced but not first acquired the lock, is unordered. Reentranreentrantlock Default is non-fair .

By the way, leave a few of the methods commonly used in lock:

Lock.getholdcount (); Queries the current thread to keep the number of locks. That is, the number of times the lock () method was called.

Lock.getqueuelength (); Returns the estimated number of threads that are waiting to acquire a secondary lock. For example, five threads, 11 threads are developing an await () method, and name returns 4 after using this method.

Lock.getwaitqueuelength (condition,condition); Returns the number of thread estimates waiting for the given condition associated with this lock condition. For example, there are five threads executing the await () method of the same condition object,

5 is returned after this method is called.

  

  Every good person, there is a period of silence, do not complain, no complaints, finally spent the period of moving their own days.

Multi-threaded Learning fair lock and unfair lock

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.