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.