Home  >  Article  >  Java  >  synchronized usage summary

synchronized usage summary

高洛峰
高洛峰Original
2016-12-13 11:12:121252browse

The impact of synchronized on the code when used in different places:
1. synchronized keyword modification method
Assume that P1 and P2 are different objects of the same class. This class defines synchronized blocks or synchronized methods in the following situations, P1 , P2 can call them.
public synchronized void method(){
​ //
}
This is the synchronized method, and what synchronized locks at this time is the call to this synchronized method object. In other words, when an object P1 executes this synchronization method in different threads, they will form mutual exclusion to achieve synchronization effect. At the same time, if there are multiple synchronized methods in the object, when a thread executes a synchronized method in the object, other synchronized methods in the object will not be allowed to be executed by other threads. However, another object P2 generated by the Class to which this object belongs can arbitrarily call this method with the synchronized keyword added.
The sample code above is equivalent to the following code:
public void method() {
synchronized (this)
{
Only the thread of P1 can call the synchronization method of P1. For P2, the lock of P1 has nothing to do with it. The program may also escape the control of the synchronization mechanism in this case, causing data confusion.
2. Synchronized block, the sample code is as follows:
public void method(SomeObject so) {
synchronized(so)
{ //..
}
}
At this time, the lock is the so object, and each object corresponds to a unique lock , so whichever thread gets the object lock can run the code he controls. When there is a clear object as the lock, you can write the program like this, but when there is no clear object as the lock and you just want to synchronize a piece of code, you can create a special instance variable (it must be an object) to act as the lock:
private byte[] lock = new byte[0];

Public void method(){
synchronized(lock) }
}

PS: A zero-length byte array object will be created faster than any object Both are economical - look at the compiled bytecode: generating a zero-length byte[] object requires only 3 opcodes, while Object lock = new Object() requires 7 lines of opcodes.

3. Apply synchronized to static functions. The sample code is as follows:
Class Foo
{ public synchronized static void method1()

{ //.
}
public void method2()
{
           synchronized(Foo.class)                                                                           /
}
}

Both of these two synchronized methods call the class lock of the class to which the object of this method belongs (Class, rather than a specific object generated by this Class).

It can be inferred: If a synchronized static function A is defined in a class, and a synchronized instance function B is also defined, then when the same object Obj of this class accesses the A and B methods respectively in multiple threads, it will not It constitutes synchronization because their locks are all different. The lock of method A is the Class to which Obj belongs, and the lock of method B is the object to which Obj belongs.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn