Parsing synchronized locked objects in Java programming

The picture upload password is changed to synchronized, which is a keyword used for synchronization in Java. Generally, we lock an object through synchronized to synchronize threads. We need to know which object synchronized locks during program execution, otherwise we may have problems in multithreaded programs.

Looking at the following code, we define a static variable n. in the run method, we increase n by 10, and then in the main method, we open up 100 threads to perform the operation added by N. if the threads do not execute concurrently, the final value of n should be 1000. Obviously, the result after the execution of the following program is not 1000, because we do not synchronize threads.

To achieve synchronization, we modify the above code and add an increase method, as follows. However, when we execute the following code, we will find that n is still not 1000

In fact, the reason is very simple. Multiple threads above do not compete for the same object lock at all. When we execute a non static method decorated with synchronized, the thread will first obtain the lock of the object calling the method, and then continue to execute the code. So which object is calling this method? This object. In the above example, the lock object obtained by the thread represented by thread [i] is the thread [i] object, that is, the thread object itself. Therefore, the 100 threads opened up above can execute as long as they obtain their own objects, which makes synchronization ineffective.

We modify the code again: change the increase method to I static. At this time, after the program is executed, the value of n is 1000.

When synchronized modifies the static method, it locks the class object of the class, not a specific object. In the above example, it locks synchronized test 3 Class object. During program execution, there is only one class object of the class, so the above threads compete for the same object lock.

The following is a summary of synchronized locked objects:

(1) For synchronization methods, lock the current object (this) (2) for static synchronization methods, lock the class object of the current class (3) for synchronization code blocks, lock the objects in synchronized parentheses

summary

The above is all about parsing the synchronized locked objects in Java programming. I hope it will be helpful to you. Interested friends can continue to refer to this site: Java programming redisson implementation of distributed lock code example, Java Concurrent Programming re-entry lock and read-write lock, etc. if you have any questions, you can leave a message directly. Xiaobian will reply to you in time. Below we recommend books related to basic Java programming on this site, which can be downloaded for free for friends to learn and reference.

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