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
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.