The use of Java multithreaded programming 4--lock--fair lock and non-fair lock

Source: Internet
Author: User

Fair and unfair Locks: lock lock is divided into "fair lock" and "non-fair lock", fair lock indicates threadThe order in which locks are acquired is assigned in the order of line Cheng. , that is, first come first get FIFO first order. And the non-fair lock is a preemptive mechanism to acquire locks, is randomly acquired locks, and the fair lock is not the same is not the first to get the lock, this way may cause some threads have not been able to get locks, the result is unfair.

1. Fair Lock Example

public class Service {    private reentrantlock lock;    Public Service (Boolean isfair) {        lock = new Reentrantlock (Isfair);    }    public void Servicemethod () {        try {            lock.lock ();            System.out.println ("threadname=" + thread.currentthread (). GetName ()                    + "get Locked");        } finally {            Lock.unlock ();}}}    
public class Run {public    static void Main (string[] args) throws Interruptedexception {        final service service = NE W Service (true);  Change to False to lock        Runnable Runnable = new Runnable () {public            void run () {                System.out.println ("* * Thread:" + Threa) for non-fairness D.currentthread (). GetName ()                        +  "Run");                Service.servicemethod ();            }        };        thread[] Threadarray = new THREAD[10];        for (int i=0; i<10; i++) {            Threadarray[i] = new Thread (runnable);        }        for (int i=0; i<10; i++) {            threadarray[i].start ();}}    }

* * Thread: Thread-0 running the
threadname=thread-0 Get Locked
* * Thread: Thread-1 running the
Threadname=thread-1 Get Locked
* * Thread: Thread-4 running the
* * Thread: Thread-5 running the
Threadname=thread-4 Get Locked
* * Thread: Thread-3 running the
* * Thread: Thread-7 running the
* * Thread: Thread-9 running the
Threadname=thread-5 Get Locked
Threadname=thread-3 Get Locked
* * Thread: Thread-2 running the
Threadname=thread-7 Get Locked
Threadname=thread-9 Get Locked
Threadname=thread-2 Get Locked
* * Thread: Thread-6 running the
Threadname=thread-6 Get Locked
* * Thread: Thread-8 running the
Threadname=thread-8 Get Locked
The result of printing is output in the order of the line Cheng, that is, the thread is running, and the lock is obtained first

--------------------------------------------------------------------------

Change the true of the run class to false for the unfair lock.

* * Thread: Thread-1 running the
* * Thread: Thread-4 running the
Threadname=thread-1 Get Locked
* * Thread: Thread-3 running the
Threadname=thread-4 Get Locked
* * Thread: Thread-6 running the
* * Thread: Thread-5 running the
* * Thread: Thread-2 running the
* * Thread: Thread-0 running the
Threadname=thread-6 Get Locked
* * Thread: Thread-7 running the
Threadname=thread-7 Get Locked
* * Thread: Thread-8 running the
Threadname=thread-8 Get Locked
Threadname=thread-3 Get Locked
* * Thread: Thread-9 running the
Threadname=thread-9 Get Locked
Threadname=thread-5 Get Locked
Threadname=thread-2 Get Locked
threadname=thread-0 Get Locked
is out-of-order, indicating that the thread starting with start () does not mean that the lock is obtained first

The use of Java multithreaded programming 4--lock--fair lock and non-fair 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.