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