Detailed explanation of the method of using condition class to operate lock in Java multithreaded programming
The function of condition is to control the lock more accurately. The await() method in condition is equivalent to the wait() method of object, the signal() method in condition is equivalent to the notify() method of object, and the signalall() method in condition is equivalent to the notifyall() method of object. The difference is that the wait(), notify(), notifyall() method in object is used in combination with the synchronized keyword; Condition needs to be used in combination with "mutex lock" / "shared lock".
Condition function list
Condition class usage example condition decomposes the object monitor methods (wait, notify, and notifyAll) into distinct objects, so as to provide multiple wait sets for each object by combining these objects with any lock implementation (wait set). Lock replaces the synchronized method and statement, and condition replaces the object monitor method. The following is an example of thread communication written before. The code is as follows:
In the condition, wait () is replaced with await (), notify () is replaced with signal (), and notifyAll () is replaced with signalall (). The traditional thread communication mode, condition, can be implemented. Note here that the condition is bound to lock, and the newcondition () method must be used to create a lock condition. In this way, condition is no different from traditional thread communication. The strength of condition is that it can establish different conditions for multiple threads. The following is a section of code in the API to explain.
This is a cache in a multi-threaded working environment. The cache provides two methods, put and take. Put is to store data and take is to retrieve data. There is a cache queue inside. See the code for specific variables and method descriptions. The functions realized by this cache class are: multiple lines store data and retrieve data from it, The maximum value that can be cached in the cache queue (first in first out, last in and last out) is 100. Multiple threads are mutually exclusive. When the value stored in the cache queue reaches 100, the write thread will be blocked and the read thread will be awakened. When the value stored in the cache queue is 0, the read thread will be blocked and the write thread will be awakened. The following is an analysis of the code execution process: 1 A write thread executes and calls the put method;
2. Judge whether the count is 100. Obviously, there is no 100;
3. Continue to execute and store the value;
4. After judging whether the currently written index position + + is equal to 100, change the written index value to 0 and count + 1; 5. Wake up only one of the read thread blocking queues;
6. A read thread executes and calls the take method;
7. ……
8. Wake up only one of the write thread blocking queues.
This is the strength of multiple conditions. Assuming that the cache queue is full, the blocking thread must be the write thread and the wake-up thread must be the read thread. On the contrary, the blocking thread must be the read thread and the wake-up thread must be the write thread. What effect will it have if there is only one condition? The cache queue is full, This lock doesn't know whether it wakes up the read thread or the write thread. If it wakes up the read thread, everyone is happy. If it wakes up the write thread, the thread has just been awakened and blocked. At this time, it wakes up again, which wastes a lot of time.