The synchronization implementation of multi-threading in java learning

Recently, I have been learning about multithreading, and I feel that multithreading is really esoteric. The more I read, the more I feel that this knowledge is really awesome. Sharing, is constantly enriching my understanding of multi-threading. The following are some summary points of my own learning.

The advantage of multithreading is that it can improve the efficiency of services and is very suitable for dealing with high concurrency issues. It improves the performance of our code. But at the same time, due to the implementation limitations of the Java virtual machine, we need to consider when implementing multithreading to improve performance. Data security, uniqueness, and visibility. Make each thread's desired result what they want. Threads do not interfere with each other directly. Here is the thread synchronization problem we have been discussing. Threads are in a shared Partially synchronized, there is no possibility of interference.

How to achieve synchronization: Java only provides two methods of synchronized and Lock

One, the implementation principle of synchronized keyword and Lock

1. The realization principle of synchronized: The synchronization is mainly achieved by the monitor assigned to the object or class by the jvm virtual machine. The thread controls the competition relationship with other threads by obtaining the two states of giving up the monitor.
2. The realization principle of Lock: It is mainly realized through the code function, and the thread synchronization is realized through the state state field. When the state is 0, the lock is acquired, the state is set to 1, the code code block is run, and when other threads come in , all put into the thread queue, wait, enter the blocking state, spin lock implemented by wireless for loop. The tryacquire() method is to check whether the lock is occupied, if not, put the thread into the exclusiveOwnerThread variable, if there is If so, execute the acquireQueued() method and put the thread in the waiting queue. The details of this method will be supplemented later in the article.


Two synchronize and Lock features:

Due to the different principles, the two methods to achieve synchronization are also different. So the characteristics are also different.

1, synchronize can modify code blocks and methods. Lock is mainly used in code blocks

2. Since lock is a synchronization implemented by code, and there are many function extension classes implemented specifically, the functions are relatively more convenient, and some complex synchronization functions are implemented through Lock.

3. In terms of performance, the specifics are not clear. According to what others have shared, the synchronization of the synchronize lock is slower before jdk5. The later version is optimized, the performance has been improved, and the Lock

The implementation is pretty much the same.

4 In use, simple synchronization can be achieved through synchronize, and complex synchronization functions can be achieved through Lock.

The level is limited, personal understanding is the main thing, and I hope God will give more advice. The specific use of the two will be shared in detail later in the blog.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324706273&siteId=291194637