Multithreaded programming learning IV (use of lock)

1、 Foreword

This article will introduce that using the lock object in Java 5 can also achieve the effect of synchronization, and it is more convenient and flexible in use, mainly including the use of reentrantlock class and reentrantreadwritelock class.

What is the difference between lock and synchronized keywords?

Waiting interruptible means that when the thread holding the lock does not release the lock for a long time, the waiting thread can choose to give up waiting and deal with other things instead. The interruptible feature is very helpful to deal with synchronization blocks with very long execution time.

Before Java 5, when a thread cannot obtain a lock and is blocked outside synchronized, interrupt the thread. At this time, the interrupt flag bit of the thread will be modified, but the thread will still block on synchronized and wait to obtain the lock; In Java 5, the AQS synchronizer provides the acquireinterruptible (int ARG) method. When waiting to obtain the synchronization status, if the current thread is interrupted, it will return immediately and throw an interruptedexception.

2、 Using the reentrantlock class

1. In Java multithreading, the synchronized keyword can be used to realize synchronization and mutual exclusion between threads, but in jdk1 The newly added reentrant lock (re-entry lock) in 5 can achieve the same effect, and is more powerful in extended functions, such as sniff locking, multi-channel branch notification and so on, and is more flexible in use than synchronized.

2. Call lock The thread of lock () code holds the "object monitor", that is, lock holds the object lock, which depends on the existence of the instance of this class.

3. The keyword synchronized can be combined with wait() and notify() / notifyall() methods to realize the wait / notification mode. The reentrantlock class can also realize the same function, but it needs the help of the condition object.

4. Multiple condition objects are used to realize selective notification between threads.

5. Fair lock and unfair lock

Fair lock ensures that the lock is obtained according to the FIFO principle at the cost of a large number of thread switches. Although unfair locking may cause thread "hunger", there are few thread switches to ensure its greater throughput.

6. Introduction to common methods of reentrantlock

(1) int getHoldCount () queries the number of threads that the current thread keeps locked, that is, the number of calls to lock in the thread.

(2) Int getqueuelength() returns the estimated number of threads waiting for this lock. For example, if there are five threads and one thread is occupying this lock for execution, then calling this method returns 4. This value is only an estimate, because the number of threads may change dynamically while this method traverses the internal data structure. This method is used to monitor system status, not for synchronous control.

(3) Int getwaitqueuelength (condition condition) returns the estimated number of threads waiting for a given condition related to this lock. For example, if there are five threads and each thread executes the await() method of the same condition object, the value returned by calling this method is 5.

(4) Boolean hasqueuedthread (thread thread) queries whether the specified thread is waiting to acquire this lock.

(5) Boolean hasqueuedthreads() queries whether a thread is waiting to acquire this lock.

(6) Boolean haswaiters (condition condition) queries whether a thread is waiting for a condition condition related to this lock

(7) Boolean isfair() determines whether the lock is fair.

(8) Boolean isheldbycurrentthread() queries whether the current thread holds this lock.

(9) Boolean islocked() queries whether this lock is held by any thread.

(10) Void lockinterruptibly() if the current thread is not interrupted, it obtains the lock. If it has been interrupted, an exception occurs. The difference between the lock () method and the lock () method is that this method will respond to interrupt, that is, the current thread can be interrupted during lock acquisition.

(11) Boolean trylock() obtains this lock only if the lock is not locked by another thread at the time of the call.

(12) Boolean trylock (long timeout, timeunit unit) if the lock is not held by another thread within a given waiting time and the current thread is not interrupted, the lock is obtained.

(13) lock. Awaituninterruptible (): this thread will not be interrupted until other threads call the signal () or signalall () methods.

(14) lock. Awaituntil (date): this thread will sleep until:

When obtaining the synchronization status, the synchronizer maintains a synchronization queue, and the threads that fail to obtain the status will be added to the queue and spin in the queue; The condition to move out of the queue (or stop spinning) is that the predecessor node is the head node and successfully obtains the synchronization state. When releasing the synchronization state, the synchronizer calls the tryrelease (int ARG) method to release the synchronization state, and then wakes up the successor node of the head node.

3、 Using the reentrantreadwritelock class

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
分享
二维码
< <上一篇
下一篇>>