What is the difference between synchronizing a static method and a non static method in java?Can anybody please explain with an example. Also is there any difference in synchronizing a method and synchronizing a block of code?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Java Thread acquires an object level lock when it enters into an instance synchronized java method and acquires a class level lock when it enters into static synchronized java method. By using synchronized block you can only lock critical section of code and avoid locking whole method which can possibly degrade performance.
Synchronization in Java is basically an implementation of monitors. When synchronizing a non static method, the monitor belongs to the instance. When synchronizing on a static method, the monitor belongs to the class. Synchronizing a block of code is the same idea, but the monitor belongs to the specified object. If you can get away with it, synchronized blocks are preferable because they minimize the time each thread spends in the critical section
There is virtually no difference between synchronizing a block and synchronizing a method. Basically:
is the same as
By comparison a static synchronized method is the same as:
I will try and add an example to make this extra clear.
As has been mentioned, synchronized in Java is an implementation of the Monitor concept. When you mark a block of code as synchronized you use an object as a parameter. When an executing thread comes to such a block of code, it has to first wait until there is no other executing thread in a synchronized block on that same object.
In the above example, a thread running
doOtherStuff()
would block another thread from entering the block of code protectingdoStuff()
. However, a thread could enter the block arounddoSomeStuff()
without a problem as that is synchronized onObject b
, notObject a
.When you use the synchronized modifier on an instance method (a non-static method), it is very similar to having a synchronized block with "this" as the argument. So in the following example,
methodA()
andmethodB()
will act the same way:Note that if you have a
methodC()
in that class which is not synchronized and does not have a synchronized block, nothing will stop a thread from entering that method and careless programming could let that thread access non-safe code in the object.If you have a static method with the synchronized modifier, it is practically the same thing as having a synchronized block with
ClassName.class
as the argument (if you have an object of that class,ClassName cn = new ClassName();
, you can access that object withClass c = cn.getClass();
)So in the above example,
staticMethodA()
andstaticMethodB()
act the same way. An executing thread will also be blocked from accessing the code block innonStaticMethodC()
as it is synchronizing on the same object.However, it is important to know that nothing will stop an executing thread from accessing
unSafeStaticMethodD()
. Even if we say that a static method "synchronizes on the Class object", it does not mean that it synchronizes all accesses to methods in that class. It simply means that it uses the Class object to synchronize on. Non-safe access is still possible.From javadoc https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html
when a static synchronized method is invoked, since a static method is associated with a class, not an object. In this case, the thread acquires the intrinsic lock for the Class object associated with the class. Thus access to class's static fields is controlled by a lock that's distinct from the lock for any instance of the class.
When we acquire a lock on any class, we actually acquire a lock on "Class" class instance which is only one for all instances of class.
we can create multiple object's of a class and each object will have one lock associated with it.
In short if you synchronize on a static method you will synchronize on the class (object) and not on an instance (object). That means while execution of a static method the whole class is blocked. So other static synchronized methods are also blocked.