Java multithreaded concurrent JUC package ReentrantLock display lock
ReentrantLock supports the following features:
- Support fair and unfair ways to acquire locks.
- Supports reentrability.
Fair locks and unfair locks:
- Fair lock: Check whether there are threads waiting in line before adding locks. If so, give priority to the threads in front, first come first served.
- Unfair lock: When a thread adds a lock, it directly tries to acquire the lock, and if it fails to obtain it, it will automatically wait at the end of the queue.
1 lock and unlock method description
The demo simulates the ticket sales situation of the cinema, and the total ticket count. 10 windows of ticket sales have been opened, and the program code is as follows:
public class ReentrantLockDemo01 implements Runnable { private Lock lock = new ReentrantLock(); private int tickets = 50; @Override public void run() { while (true) { // Get the lock if (()) { try { if (tickets > 0) { (100); (().getName() + " " + tickets--); } else { break; } } catch (InterruptedException e) { (); } finally { (); // Release } } } } public static void main(String[] args) { ReentrantLockDemo01 reentrantLockDemo = new ReentrantLockDemo01(); for (int i = 0; i < 10; i++) { Thread thread = new Thread(reentrantLockDemo, "thread - " + i); (); } } }
The output is as follows:
thread - 0 50
thread - 7 49
thread - 4 48
thread - 7 47
thread - 7 46
thread - 7 45
thread - 7 44
thread - 7 43
thread - 7 42
thread - 7 41
thread - 7 40
thread - 7 39
thread - 7 38
thread - 7 37
thread - 7 36
thread - 7 35
thread - 7 34
thread - 7 33
thread - 7 32
thread - 7 31
thread - 7 30
thread - 5 29
thread - 5 28
thread - 5 27
thread - 6 26
thread - 6 25
thread - 7 24
thread - 7 23
thread - 7 22
thread - 7 21
thread - 5 20
thread - 5 19
thread - 5 18
thread - 7 17
thread - 2 16
thread - 2 15
thread - 2 14
thread - 2 13
thread - 1 12
thread - 1 11
thread - 1 10
thread - 1 9
thread - 1 8
thread - 1 7
thread - 1 6
thread - 1 5
thread - 1 4
thread - 1 3
thread - 1 2
thread - 1 1
2 newCondition method
The function of Condition is to provide more precise control of the lock.
In Conditionawait()
The method is equivalent to Objectwait()
Method, in Conditionsignal()
The method is equivalent to Objectnotify()
Method, in ConditionsignalAll()
Equivalent to ObjectnotifyAll()
method.
The difference is that thewait()
, notify()
, notifyAll()
The method is used in bundled with the synchronized keyword; and Condition needs to be used in bundled with the "mutex"/"shared lock".
/** * Producer and consumer */ public class ProducerConsumerTest { private Lock lock = new ReentrantLock(); private Condition addCondition = (); private Condition removeCondition = (); private LinkedList<Integer> resources = new LinkedList<>(); private int maxSize; public ProducerConsumerTest(int maxSize) { = maxSize; } public class Producer implements Runnable { private int proSize; private Producer(int proSize) { = proSize; } @Override public void run() { (); try { for (int i = 1; i < proSize; i++) { while (() >= maxSize) { ("The current warehouse is full, waiting for consumption..."); try { (); } catch (InterruptedException e) { (); } } ("Number of products have been produced: " + i + "\tTotal current storage volume:" + ()); (i); (); } } finally { (); } } } public class Consumer implements Runnable { @Override public void run() { String threadName = ().getName(); while (true) { (); try { while (() <= 0) { (threadName + "There is no product in the current warehouse, please wait..."); try { // Enter blocking state (); } catch (InterruptedException e) { (); } } // Consumption data int size = (); for (int i = 0; i < size; i++) { Integer remove = (); (threadName + "The current consumer product number is:" + remove); } // Wake up the producer (); } finally { (); } } } } public static void main(String[] args) throws InterruptedException { ProducerConsumerTest producerConsumerTest = new ProducerConsumerTest(10); Producer producer = Producer(100); Consumer consumer = Consumer(); final Thread producerThread = new Thread(producer, "producer"); final Thread consumerThread = new Thread(consumer, "consumer"); (); (2); (); } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.