Java multithreading – thread synchronization

When multiple threads operate on the same object, it is easy to cause thread safety problems. In order to solve the problem of thread safety, Java multithreading introduces synchronization monitor.

Synchronous code block

The syntax format of synchronization code block is as follows:

Obj in parentheses after synchronized in the above syntax format is the synchronization monitor. The meaning of the above code is that the thread must obtain the lock on the synchronization monitor before starting to execute the synchronization code block.

Only one thread can obtain the lock on the synchronization monitor at any time. When the execution of the synchronization code block is completed, the thread naturally gives up the lock on the synchronization monitor.

It is generally recommended to use shared resources that may be accessed concurrently as synchronization monitors.

Synchronization method

A synchronization method is to modify a method with the synchronized keyword, which is called a synchronization method. For the synchronization method, there is no need to explicitly specify the synchronization monitor. The synchronization monitor of the synchronization method is this, that is, the current object itself.

It should be noted that the synchronized keyword can modify code blocks and methods, but not constructors, attributes, etc.

Thread safety of variable classes is at the cost of reducing program running efficiency. In order to reduce the negative impact of thread safety, the following strategies can be used:

Unlock synchronization monitor

After a thread enters the synchronization method or synchronization code block, it cannot actively release the lock on the synchronization monitor. The thread will release the lock on the synchronization monitor under the following circumstances:

The program will not release the lock of the synchronization monitor in the following cases:

Synchronous lock

Synchronization lock realizes thread synchronization by displaying and defining synchronization lock objects. Here, the lock object is used as the synchronization lock.

It is generally believed that synchronous locks provide a wider range of locking operations than synchronous code blocks and synchronous methods.

The use of synchronized methods or statements provides access to implicit monitor locks associated with each object, but forces all lock acquisitions and releases to occur in a block structure: when multiple locks are acquired, they must be released in the opposite order, and all locks must be released within the same lexical range as when all locks are acquired.

Although the scope mechanism of synchronized methods and statements makes it much easier to use monitor lock programming, and also helps to avoid many common programming errors involving locks, sometimes locks need to be used in a more flexible way. For example, some algorithms that traverse the data results of concurrent access require the use of "hand over hand" or "chain locking": obtain the lock of node a, then obtain the lock of node B, then release a and obtain C, then release B and obtain D, and so on. The implementation of lock interface allows locks to be acquired and released in different scopes, and allows multiple locks to be acquired and released in any order, so as to support the use of this technology.

The lock implementation provides other functions that are not available by using synchronization methods and synchronization code blocks, including a non block structure attempt to obtain locks (trylock ()), an attempt to obtain interruptible locks (lockinterruptible ()), and an attempt to obtain timeout expired locks (trylock (long, timeunit)).

The lock class can also provide behavior and semantics completely different from implicit monitor locks, such as guaranteed sorting, non reentrant usage, or deadlock detection.

Reentrantlock is commonly used as the implementation class of lock. Reentrant lock has the same reentrant property, that is, a thread can lock the reentrant lock it has locked again.

There is also an interface readwritelock. Readwritelock maintains a pair of related locks, one for read-only operations and the other for write operations. As long as there is no writer, the read lock can be held by multiple reader threads at the same time. Write locks are exclusive. All readwritelock implementations must ensure the memory synchronization effect of writelock operation and keep in touch with relevant readlocks. That is, the thread that successfully acquires the read lock will see all updates made by the version before writing the lock.

deadlock

When two threads wait for each other to release synchronization, the monitor will deadlock. The Java virtual machine does not monitor or take measures to deal with deadlock.

To prevent deadlock, you need to prevent threads from occupying resources while waiting.

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