In the previous article we talked about how to use keyword synchronized to achieve synchronous access. In this article we continue to explore this issue, from Java 5, after the Java.util.concurrent.locks package provides another way to achieve synchronous access, that is lock.
Some friends may ask, since all can be achieved through synchronized to achieve synchronous access, then why need to provide lock. This issue will be described below. This article starts with the flaw of the synchronized, then tells about the class and the interface which is commonly used under the Java.util.concurrent.locks package, and finally discusses the following things about the concept of lock
The following is an outline of this table:
I. Deficiencies in synchronized
Two. Commonly used classes under the Java.util.concurrent.locks package
Three. Introduction to the concept of lock related
If there are any mistakes please forgive and welcome to criticize.
Please respect the results of the work of the author, reproduced please indicate the original link:
Http://www.cnblogs.com/dolphin0520/p/3923167.html A. Synchronized's flaws
Synchronized is a keyword in Java, which is a built-in feature of the Java language. So why would there be lock?
In the previous article, we learned that if a block of code is synchronized decorated, when a thread acquires the corresponding lock and executes the code block, the other threads can only wait until the thread that acquires the lock releases the lock, and there are only two situations where the thread that acquires the lock releases the lock:
1 The thread that gets the lock executes the code block, and then the thread releases the possession of the lock;
2 The thread executes an exception, at which point the JVM allows the thread to automatically release the lock.
So if the thread that gets the lock is blocked by waiting for IO or some other reason (such as calling the Sleep method), but without releasing the lock, the other threads will just have to wait, and imagine how this affects program execution efficiency.
So there is a mechanism to keep waiting threads from waiting indefinitely (such as just waiting for a certain amount of time or being able to respond to interruptions), which can be done by lock.
Another example: when more than one thread reads and writes a file, there is a conflict between read and write operations, and write and write operations conflict, but read and read operations do not conflict.
But using the Synchronized keyword to achieve synchronization can lead to a problem:
If more than one thread is read-only, other threads can only wait for read operations to be performed while a thread is reading.
Therefore, a mechanism is needed to allow multiple threads to be read only, and there is no conflict between threads, which can be done by lock.
In addition, through lock you can know that the line Chengyu did not successfully acquire the lock. This is synchronized impossible to do.
To summarize, this means that lock provides more functionality than synchronized. However, the following points should be noted:
1 lock is not built into the Java language, synchronized is a keyword in the Java language and is therefore a built-in feature. Lock is a class that enables synchronous access through this class;
2 lock and synchronized have a very big difference, the use of synchronized does not require the user to manually release the lock, when the synchronized method or synchronized code block execution, the system will automatically let the thread release the lock on the occupation , and lock must have the user to manually release the lock, if there is no active release of the lock, it can lead to a deadlock phenomenon. two. Commonly used classes under the Java.util.concurrent.locks package
Let's explore the classes and interfaces commonly used in java.util.concurrent.locks packages.
1.Lock
The first thing to say is lock, and by looking at the lock source, lock is an interface:
1 2 3 4 5 6 7 8 |
public interface Lock {void lock (); void Lockinterruptibly () throws interruptedexception; Boolean Trylock (); Boolean Trylock (long time, Timeunit unit) throws Interruptedexception; void unlock (); |