Java_ concurrent thread _lock, Readwritelock

Source: Internet
Author: User

1.Lock

The Reentrantlock is the implementation class for the lock interface, which should be used with lock. If the lock lock is not occupied by another thread, the current thread can obtain a lock lock immediately. The same thread can call lock several times before the lock lock is released, using Getholdcount () to get the current thread lock lock number of times. Isheldbycurrentthread () determines whether the current thread has a lock lock.

(1). Use formatting
Be sure to call Lock.unlock () in finally to unlock and prevent deadlocks.
Lock L = ...; L.lock (); try {//access the resource protected by this lockfinally {L.unlock ();}}

(2). Common Methods
1). void Lock ();

Attempts to obtain a lock lock, if the lock is held by another thread, the current thread is not executing, nor can it be dispatched until the lock is reached;

2). void lockinterruptibly () throws Interruptedexception

Attempt to acquire the lock, if the lock can be obtained, then return immediately. If the lock is obtained, then the thread stops executing and cannot be re-dispatched until the current thread is interrupt, throwing an exception;

3). Boolean Trylock ()

Try to get the lock, if successful then lock the object and return True, otherwise return false, this method will return immediately;

4). Boolean Trylock (long time, Timeunit unit) throws Interruptedexception

Trylock similar, waiting for maximum time to try to acquire a lock;

5). Unlock ()

Release the lock, minus one

6). Newcondition ()

Refer to Java_ concurrent Threading _condition article

(3). The difference from synchronized

Synchronized lock release is a language built-in, does not appear to forget to release the lock, in addition, because it is a language built-in support, debugging is able to quickly know which thread the lock is held, it locks the number of times. And lock is just a normal class, so the debugger doesn't know anything about the lock, it's just a normal object (you can watch the stack frame of each thread to see it waiting for the lock, of course), but lock is more flexible and extensible.
So it's recommended: if you're just trying to achieve mutual exclusion, use synchronized and lock if you want to add functionality to the lock.

2.ReadWriteLock

Reentrantreadwritelock is the implementation class for the Readwritelock interface, which is used by the mating thread.

(1). read lock and write lock conditions
1). Different
thread

Read locks are not mutually exclusive, read locks are mutually exclusive to write locks, and write locks are mutually exclusive to write locks.

2). Same
thread

Read lock can get multiple getreadholdcount (), write lock can get multiple getwriteholdcount;

Write locks cannot be acquired under Read locks, but read locks can be obtained in advance of write locks.

(2). Format

class Cacheddata {Object data;volatile boolean cachevalid;final reentrantreadwritelock rwl = new Reentrantreadwritelock (); void Processcacheddata () {Rwl.readlock (). Lock (); if (!cachevalid) {//must release read lock Before acquiring write Lockrwl.readlock (). Unlock (); Rwl.writelock (). Lock (); try {//recheck state because another thread Might have//acquired write lock and changed state before we Did.if (!cachevalid) {//data = ... cachevalid = true;//downg Rade by acquiring read lock before releasing//write Lockrwl.readlock (). Lock ();}} finally {Rwl.writelock (). unlock ();//unlock Write, still hold read}}try {//use (data);} finally {Rwl.readlock (). Unlock ()
  ;}}}

(3). Use note

Read locks are write-lock operations, read locks do not read the lock operation, multiple read locks can be concurrent non-blocking. That is, the write lock cannot be obtained by any thread until the read lock is acquired and the read lock is released.

While multiple read locks are in effect, the thread attempting to acquire a write lock is in a wait state, and when the last read lock is released, the thread attempting to acquire the write lock has the opportunity to acquire a write lock.

Write locks are write-lock, row-read lock operation. When a thread acquires a write lock, other threads that attempt to acquire a write lock and attempt to acquire a read lock are waiting until the write lock is freed.

A write lock can be read-locked, i.e.:

Rwl.writelock (). Lock ();//In the Write lock state, you can get the read lock Rwl.readlock (). Lock (); Rwl.writelock (). Unlock ();
A read lock is not able to obtain a write lock, and if a write lock is to be added, the thread must release the read lock it holds, namely:

Rwl.readlock (). Lock ();//......//must release the read lock to be able to write the lock Rwl.readlock (). Unlock (); Rwl.writelock (). Lock ();

Java_ concurrent thread _lock, Readwritelock

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.