Please Explain How Java synchronized work with static method? [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java synchronized methods: lock on object or class
Please Explain How Java synchronized work with static method ? Some one has said that it is done by its Class Object but they don't say how the lock is done with that.

You always synchronize on a monitor object. Every Java object can be used here.
With a synchronized block, you can specify that object directly.
synchronized (something){
}
With a synchronized method, it synchronizes on the object instance itself (on this), so it is identical to:
synchronized (this) {
}
With a synchronized static method, it synchronizes on the class object, just like "some one has said".
synchronized (ThisClass.class){
}
The mechanism is always the same: Only one thread can hold the lock at the same time, others have to wait.

Related

What objects to synchronize on? Why are local variables bad? [duplicate]

This question already has answers here:
Clarification on the meaning of the parameter block synchronization
(3 answers)
What does 'synchronized' mean?
(17 answers)
How does synchronized work in Java
(4 answers)
Closed 2 years ago.
There is a lot of material on stack-overflow about synchronization, but I still haven't acquired quality content about deciding which object to use as an intrinsic lock. Can some one actually make a good answer as a rule of thumb?
So should I choose 'monitor' as an instance variable or local variable or instance owning the method? All three of them do the job well. Also primitive value wrapper classes use 'pools' so no problem there as well, as threads 'attack' the same lock.
So why is it better to do this (this):
class A {
void methodA(){
synchronized (this){
//some code
}
}
}
over this(instance variable):
class A {
String monitor = "monitor";
void methodA(){
synchronized (monitor){
//some code
}
}
}
or over this(local variable):
class A {
void methodA(){
String monitor = "monitor";
synchronized (monitor){
//some code
}
}
}
They all work fine/same. So why did I read that I should avoid local variables when they implicitly use pools to store objects? What matter does the scope of variables make in this case?
Thanks!
You should avoid using the monitor of objects stored in local variables because typically only the current thread has access to objects stored in local variables. But since in this particular case, the local variable actually holds a globally shared object from the constant pool, you don't suffer from that particular problem.
The problem with using monitors of constant pool objects like here:
String monitor = "monitor";
void methodA() {
synchronized (monitor){
//some code
}
}
... is that there is only one pooled constant object.
Two different threads operating on two different instances of class A cannot enter the synchronized block in methodA at the same time, even if you've ensured it should be safe (for example, you don't touch static shared state).
What's even worse: there might be some other class B somewhere else, which also happens to synchronize on the constant "monitor" string. Now a thread using class B will block other, unrelated, threads from using class A.
On top of that, it's incredibly easy to create a deadlock because you are unknowingly sharing locks between threads.

Synchronizing multiple blocks within same method [duplicate]

This question already has answers here:
Java synchronize block on same object in different methods
(2 answers)
Closed 2 years ago.
Suppose I have synchronized two parts of code within a method. So block1 and block2 each have keyword 'synchronized' around them, and both use 'this', meaning both blocks are guarded by same object lock.
Now if block1 is being executed by a thread, does it mean no other thread can execute block2?
Synchronized on the method declaration is the same as:
public void method() {
synchronized (this) {
// method code
}
}
Having said that, as you can see in the oracle docs you can see an example with some synchronized methods and it says:
First, it is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
So, yes, no other thread can execute block2 in that case.

If we make a method synchronized then all the instance method will be locked or only the synchronized methods will be locked? [duplicate]

This question already has answers here:
How does synchronized work in Java
(4 answers)
Closed 5 years ago.
If we make a method synchronized then all the instance method will be locked while executing that method or only the other synchronized methods will be locked?
Well, if the synchronized method is static then it will be synchronized for all instances, otherwise it will be synchronized for a specific object which called it.
if 1 object tried to access the synchronized method and the object has two calls to that method then the second call can't be proceed until the first operation is done

What does "synchronize" exactly do? [duplicate]

This question already has answers here:
What does 'synchronized' mean?
(17 answers)
Closed 8 years ago.
I have a question which may sound very basic but here it is. As commonly known in Java the synchronize keyword is used to deal with multiples threads accessing one particular instance. Now imagine if an instance A has a synchronized method do(). Does that mean that if a thread T1 executes the method do() and therefore acquires the lock of A, no other thread will access the instance A until T1 releases the lock (even if other methods are not synchronized)? or it means that all not-synchronized methods (or blocks of code) are accessible except that particular do() method which maybe executed by only one thread at a time?
Straight from the Java documentation:
It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.
Therefore, your latter explanation is correct.
If T1 gets a lock on method do() i.e method is under synchronized block.
and other portion of program say method display() is not synchronized then other threads can access this method.
So your or is correct.
A synchronized method ensures that this method is not called for more than one object instances simultaneously and also during execution of a synchronized method all the associated instance variables are refreshed before starting executing the method.

synchronized(this) vs synchronized(MyClass.class) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java Synchronized Block for .class
I was reading through an article on synchronization. I am confused on below points and need more clarification
For synchronization block. How
synchronized (this) {
// code
}
differs from
synchronized (MyClass.class) {
// code
}
Synchronizing instance method means threads will have to get exclusive lock on the instance, while synchronizing static method means thread will have to acquire a lock on whole class(correct me if I am wrong). So if a class has three methods and one of them is static synchronized then if a thread acquires lock on that method then that means it will acquire lock on the whole class. So does that mean the other two will also get locked and no other method will be able to access those two methods as the whole class is having lock?
MyClass.class and this are different things, they are different references to different objects.
this - is a reference to this particular instance of the class, and
MyClass.class - is a reference to the MyClass description object.
These synchronization blocks differ in that the first will synchronize all threads that deal concretely with this instance of MyClass, and the second one will synchronize all threads independently of which object on which method was called.
The first example (acquiring lock on this) is meant to be used in instance methods, the second one (acquiring lock on class object) -- in static methods.
If one thread acquires lock on MyClass.class, other threads will have to wait to enter the synchronized block of a static method that this block is located in. Meanwhile, all of the threads will be able to acquire lock for a particular instance of this class and execute instance methods.

Categories