Can two threads call two different synchronized instance methods of an Object?
No, If an object has synchronized instance methods then the Object itself is used a lock object for controlling the synchronization. Therefore all other instance methods need to wait until previous method call is completed.
I have 4 methods (m1, m2, m3 and m4) in a class. m1, m2 and m3 are synchronized method. Also i have 4 threads t1, t2, t3 and t4 respectively. If t1 access the m1 method (synchronized method), could t2 thread access m2 method (synchronized method) simultaneously?
If the methods are synchronized on the same monitor, then they cannot execute simultaneously in different threads. When the second thread comes to the monitor entry (the start of the synchronized method in this case), it will block until the first thread releases the monitor.
The methods (instance) are of same class. So whenever a thread enters into java synchronized method or block it acquires a lock (the object on which the method is called). So other method cannot be called at the same time on the same object until the first method is completed and lock (on object) is released.
Can a thread call a non-synchronized instance method of an Object when a synchronized method is being executed?
Yes, a Non synchronized method can always be called without any problem. In fact Java does not do any Check for a non-synchronized method. The Lock object check is performed only for synchronized Methods/blocks. In case the method is not declared synchronized Java will call even if you are playing with shared data. So you have to be careful while doing such thing. The decision of declaring a method as synchronized has to be based on critical section access. If your method does not access a critical section (shared resource or data structure) it need not be declared synchronized.
What happens when I make a static method as synchronized?
Synchronized static methods have a lock on the class "Class", so when a thread enters a synchronized static method, the class itself gets locked by the thread monitor and no other thread can enter any static synchronized methods on that class. This is unlike instance methods, as multiple threads can access "same synchronized instance methods" at same time for different instances.
Can a thread call multiple synchronized methods on the object of which it hold the lock?
Yes. Once a thread acquires a lock in some object, it may call any other synchronized method of that same object using the lock that it already holds. The locks are Reentarant.
Can two threads call two different static synchronized methods of the same class
No. The static synchronized methods of the same class always block each other as only one lock per class exists. So no two static synchronized methods can execute at the same time.
Does a static synchronized method block a non-static synchronized method?
No, the thread executing the static synchronized method holds a lock on the class and the thread executing the non-static synchronized method holds the lock on the object on which the method has been called, these two locks are different and these threads do not block each other.
Can two threads at same time execute two different methods one is static synchronized and second is only synchronized method?
Yes, both the calls will execute successfully.
When a static synchronized method is called it gets a lock on the Class object
of that class. So, no other thread can call that method or any other
static synchronized method in that class, but can call only synchronized or
non-synchronized methods.
In case of only synchronized method, it obtains the lock on the instance on
which that method is called. So, no other thread can call that method or any
other only synchronized method. But can call other static synchronized method or
non-synchronized method. When a static synchronized method is called it gets a lock on the Class object
of that class. So, no other thread can call that method or any other
static synchronized method in that class, but can call only synchronized or
non-synchronized methods.
In case of only synchronized method, it obtains the lock on the instance on
which that method is called. So, no other thread can call that method or any
other only synchronized method. But can call other static synchronized method or
Comments
Post a Comment