Condition condition of Java concurrency_ Power node Java college sorting

Condition introduction

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 example

Example 1 demonstrates the thread's sleep / wake function through object's wait(), notify().

Example 2 demonstrates the sleep / wake function of a thread through condition's await(), signal().

Example 3 is an advanced function through condition.

Example 1

Example 2

Operation results:

Through "example 1" and "example 2", we know that the methods of condition and object have the following correspondence:

Object condition hibernate wait await wakes up one thread notify signal wakes up all threads notifyAll signalall

In addition to supporting the above functions, condition is more powerful in that it can more finely control the sleep and wake-up of multithreads. For the same lock, we can create multiple conditions and use different conditions in different situations.

For example, if multiple threads read / write to the same buffer: wake up the "read thread" after writing data to the buffer; Wake up the "write thread" after reading data from the buffer; And when the buffer is full, "write thread" needs to wait; When the buffer is empty, the "read thread" needs to wait. If you use wait(), notifyall() in the object class to implement the buffer, when you need to wake up the "read thread" after writing data to the buffer, it is impossible to explicitly wake up the "read thread" through notify() or notifyall(), but you can only wake up all threads through notifyAll (but notifyAll cannot distinguish whether the awakened thread is a read thread or a write thread). However, through condition, the wake-up read thread can be explicitly specified.

Looking at example 3 below, you may have a deeper understanding of this concept.

Example 3

(a) operation result:

p1 put 1 p4 put 4 p5 put 5 p0 put 0 p2 put 2 t0 take 1 p3 put 3 t1 take 4 p6 put 6 t2 take 5 p7 put 7 t3 take 0 p8 put 8 t4 take 2 p9 put 9 t5 take 3 t6 take 6 t7 take 7 t8 take 8 t9 take 9

Result description:

(01) boundedbuffer is a buffer with a capacity of 5. Object objects are stored in the buffer. It supports multi-threaded read / write buffer. When multiple threads operate "one boundedbuffer object", they mutually exclusive access to the buffer items through the mutex lock; Moreover, all threads under the same boundedbuffer object share the two conditions of "notfull" and "notempty".

Notfull is used to control the write buffer and notempty is used to control the read buffer. When the buffer is full, the thread calling put will execute notfull Await() to wait; When the buffer is not full, the object is added to the buffer and the capacity of the buffer is count+1. Finally, the notEmpty. is called. Signal() buffers the waiting thread on notempty (the thread calling notempty.await). In short, notfull controls "buffer write". When data is written to the buffer, it will wake up the waiting thread on notempty.

Similarly, notempty controls "buffer read". When the buffer data is read, it will wake up the waiting thread on notfull.

(02) in the main function of conditiontest2, start 10 "write threads" to continuously write data to the boundedbuffer (write 0-9); At the same time, 10 "read threads" are also started to continuously read data from the boundedbuffer.

(03) briefly analyze the operation results.

1. P1 thread writes 1 to the buffer. At this time, the buffer data: | 1 | | ||

2. P4 thread writes 4 to the buffer. At this time, the buffer data: | 1 | 4 | ||

3. P5 thread writes 5 to the buffer. At this time, the buffer data: | 1 | 4 | 5 ||

4. P0 thread writes 0 to the buffer. At this time, the buffer data: | 1 | 4 | 5 | 0 ||

5. P2 thread writes 2 to the buffer. At this time, the buffer data: | 1 | 4 | 5 | 0 | 2|

At this time, the buffer capacity is 5; Buffer full! If another "write thread" wants to write data to the buffer at this time, it will call notfull in put Await() waits until the direct buffer is not full before continuing.

6. T0 thread fetches data 1 from the buffer. At this time, the buffer data: | 4 | 5 | 0 | 2|

7. P3 thread writes 3 to the buffer. At this time, the buffer data: | 3 | 4 | 5 | 0 | 2|

8. T1 thread fetches data from the buffer 4. At this time, the buffer data: | 3 | 5 | 0 | 2|

9. P6 thread writes 6 to the buffer. At this time, the buffer data: | 3 | 6 | 5 | 0 | 2 |

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>