Java concurrent programming (6) Explicit lock, java concurrency

Source: Internet
Author: User

Java concurrent programming (6) Explicit lock, java concurrency
Explicit Lock 1. Lock and ReentrantLock

Lock provides an unconditional, round-robin, timed, and disruptive Lock acquisition operation. All Lock and Unlock methods are explicit.

ReentrantLock implements Lock: it provides the same memory semantics as synchronized, and also provides reentrant locking semantics.

1. Basic Semantics:
Void LockDefinition () {Lock lock = new ReentrantLock (); try {// do someting // update object status to catch exceptions; and restore the immutability condition when necessary} finally {// release the lock in finally. unlock ();}}

 

2. Round Robin locks and timed locks

Lock. tryLock ([Long, TimeUnit]): attempts to obtain the lock, with the time being the timed lock

3. interrupted lock acquisition operations

Lock. lockInterruptibly ();

Ii. Fairness

ReentrantLock can create fair locks (obtained locks in the Request order) and unfair locks (can be inserted ).

Queuing: When a thread requests an unfair lock, if the lock status is available when a request is sent, the thread will not be put into the queue, and all the waiting threads in the queue will be skipped and the lock will be obtained;

Note: unfair locks do not advocate queuing, but cannot prevent queuing. Fair locks are placed in the queue for sequential execution.

Unfair locks are more competitive than fair locks because of a serious delay between recovering a thread in the queue and starting the thread.

Iii. read-write lock

ReentrantLock is a standard mutex lock, but it cannot be implemented in some scenarios, such as reading and writing.

ReadWriteLock read/write lock: Implements ReentrantReadWriteLock: readLock and writeLock write locks

1. Interaction and implementation methods

Release first: When a write lock is released and the queue has both read and write, should read, write be the first thread to send the request?

Read thread insertion: The current read lock is waiting for a write lock. Do you want to directly read the lock later? Direct Read increases concurrency, but may result in insufficient access to write resources.

Re-entry: Can I re-import the Read and Write locks?

Downgrade: Write lock. can I obtain the read lock without releasing the lock to downgrade the Lock of this thread?

Upgrade: can read locks take precedence over other waiting threads to upgrade to write locks? If the two threads attempt to upgrade to the write lock at the same time, the deadlock may easily occur.

Applicable to: read-only data structures

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.