Fair lock and unfair lock __ Multi-Threaded

Source: Internet
Author: User

reproduced in "Java Multithreading core technology"

Defined:
Fair Lock: According to the Order of line Cheng, that is, first to get FIFO;
Non-fair Lock: A preemption mechanism for acquiring locks, which is randomly obtained, so that some threads will not be able to get the lock, and the result is unfair.

1. Fair Lock Instance Code:

Package test.


THREAD4;

Import Java.util.concurrent.locks.ReentrantLock;

    Class service{private Reentrantlock lock; The Public Service (Boolean isfair) {lock = new Reentrantlock (isfair);//true for Fair lock} public void Servicemethod (
            ) {try{lock.lock ();
        System.out.println (Thread.CurrentThread (). GetName () + "get Lock");
        }finally {lock.unlock (); }} public class Faielock {public static void main (string[] args) {Final service service = new service
        (true); Runnable Runnable = new Runnable () {public void run () {System.out.println (Thread.currentthre
                AD (). GetName () + "Run");
            Service.servicemethod ();
        }
        };
        thread[] threads = new THREAD[10];
        for (int i=0;i<10;i++) {Threads[i] = new Thread (runnable);
        for (int i = 0; I <10 i++) {Threads[i].start ();
 }
    }
}

Output results:

Thread-0 running.
Thread-1 running.
Thread-2 running.
Thread-1 Get Lock
Thread-2 Get Lock
Thread-0 Get Lock
Thread-3 running.
Thread-3 Get Lock
Thread-4 running.
Thread-4 Get Lock
Thread-5 running.
Thread-5 Get Lock
Thread-6 running.
Thread-6 Get Lock
Thread-8 running.
Thread-8 Get Lock
Thread-7 running.
Thread-7 Get Lock
Thread-9 running.
Thread-9 Get Lock

2. Non-fair Lock sample code:

Package test.


THREAD4;

Import Java.util.concurrent.locks.ReentrantLock;

    Class service{private Reentrantlock lock; The Public Service (Boolean isfair) {lock = new Reentrantlock (isfair);//true for Fair lock} public void Servicemethod (
            ) {try{lock.lock ();
        System.out.println (Thread.CurrentThread (). GetName () + "get Lock");
            }finally {System.out.println (Thread.CurrentThread (). GetName () + "release lock");
        Lock.unlock (); }} public class Faielock {public static void main (string[] args) {Final service service = new service (false)//unjust lock Runnable Runnable = new Runnable () {public void run () {System.out.pri
                Ntln (Thread.CurrentThread (). GetName () + "Run");
            Service.servicemethod ();
        }
        };
        thread[] threads = new THREAD[10];
        for (int i=0;i<10;i++) {Threads[i] = new Thread (runnable); for (int i = 0; I <10;
        i++) {Threads[i].start ();
 }
    }
}

The run result may be

Thread-0 running.
Thread-0 Get Lock
Thread-1 running.
Thread-0 released the lock.
Thread-2 running.
Thread-2 Get Lock
Thread-2 released the lock.
Thread-1 Get Lock
Thread-1 released the lock.
Thread-3 running.
Thread-3 Get Lock
Thread-3 released the lock.
Thread-5 running.
Thread-5 Get Lock
Thread-5 released the lock.
Thread-6 running.
Thread-6 Get Lock
Thread-6 released the lock.
Thread-7 running.
Thread-7 Get Lock
Thread-7 released the lock.
Thread-8 running.
Thread-8 Get Lock
Thread-8 released the lock.
Thread-9 running.
Thread-9 Get Lock
Thread-9 released the lock.
Thread-4 running.
Thread-4 Get Lock
Thread-4 released the 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.