Java Concurrent Programming: lock

Java Concurrent Programming: lock

In the last article, we talked about how to use the keyword synchronized to achieve synchronous access. In this article, we continue to explore this problem, after Java 5, in Java util. concurrent. Another way to achieve synchronous access is provided under the locks package, which is lock.

Some friends may ask, since you can achieve synchronous access through synchronized, why do you need to provide lock? This problem will be described below. This article starts with the defect of synchronized, and then discusses Java util. concurrent. Which classes and interfaces are commonly used under the locks package? Finally, we discuss the following concepts about locks

The following is the outline of this article:

I Synchronized defects

II java. util. concurrent. Common classes under locks package

III Introduction to related concepts of lock

If there is anything wrong, please forgive me and welcome criticism and correction.

Please respect the author's labor achievements. Please indicate the original link for Reprint:

   http://www.cnblogs.com/dolphin0520/p/3923167.html

I Synchronized defects

Synchronized is a keyword in Java, that is, it is a built-in feature of the Java language. So why lock?

In the previous article, we learned that if a code block is modified by synchronized, when a thread obtains the corresponding lock and executes the code block, other threads can only wait all the time. The thread that obtains the lock can release the lock. Here, the thread that obtains the lock can only release the lock in two cases:

1) the thread obtaining the lock executes the code block, and then the thread releases the possession of the lock;

2) when an exception occurs in thread execution, the JVM will let the thread automatically release the lock.

Then, if the thread that obtains the lock is blocked due to waiting for IO or other reasons (such as calling the sleep method), but does not release the lock, other threads can only wait dryly. Imagine how this affects the execution efficiency of the program.

Therefore, a mechanism is needed to prevent the waiting thread from waiting indefinitely (for example, only waiting for a certain time or being able to respond to interrupts), which can be done through lock.

Another example: when multiple threads read and write files, the read operation and write operation will conflict, and the write operation and write operation will conflict, but the read operation and read operation will not conflict.

However, using the synchronized keyword to achieve synchronization will lead to a problem:

If multiple threads are only reading, when one thread is reading, other threads can only wait and cannot read.

Therefore, a mechanism is needed so that when multiple threads are only reading, there will be no conflict between threads, which can be done through lock.

In addition, you can know whether the thread has successfully obtained the lock through lock. This is something synchronized can't do.

To sum up, lock provides more functions than synchronized. However, the following points should be noted:

1) lock is not built-in in the Java language. Synchronized is the keyword of the Java language, so it is a built-in feature. Lock is a class through which synchronous access can be realized;

2) there is a big difference between lock and synchronized. Using synchronized does not require the user to manually release the lock. When the synchronized method or synchronized code block is executed, the system will automatically let the thread release the occupation of the lock; Lock requires the user to release the lock manually. If the lock is not released actively, it may lead to deadlock.

II java. util. concurrent. Common classes under locks package

Let's discuss Java util. concurrent. Classes and interfaces commonly used in locks package.

  1. Lock

The first thing to explain is lock. By checking the source code of lock, we can see that lock is an interface:

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