Reputation: 75
I was ask in an interview that with respect to multi threading , Suppose you have 2 threads (Thread-1 and Thread-2) on same object. Thread-1 is in synchronized method1(), can Thread-2 enter synchronized method2() at same time in java by any way .
I replied no No, here when Thread-1 is in synchronized method1() it must be holding lock on object’s monitor and will release lock on object’s monitor only when it exits synchronized method1(). So, Thread-2 will have to wait for Thread-1 to release lock on object’s monitor so that it could enter synchronized method2().
but still please advise is there any way by which Thread-2 ,enter synchronized method2() at same time in java by any way is there any hack if to achieve this thing
below is my program , rite now I have changed the implementation now please advise on this as the output of the below program is
inside M1()
t1--->RUNNABLE
inside M2()
t2--->RUNNABLE
below is my updated code
public class Test {
private final Object lockA = new Object();
private final Object lockB = new Object();
public void m1() {
synchronized(lockA) {
try {
System.out.println("inside M1()");
Thread.sleep(100);
}
catch (InterruptedException ie)
{}
}
}
public void m2() {
synchronized(lockB) {
try {
System.out.println("inside M2()");
Thread.sleep(100); }
catch (InterruptedException ie) {}
}
}
public static void main(String[] args) throws InterruptedException {
final Test t = new Test();
Thread t1 = new Thread()
{ public void run() { t.m1(); } };
Thread t2 = new Thread()
{ public void run() { t.m2(); } };
t1.start();
//Thread.sleep(500);
t2.start();
// Thread.sleep(500);
System.out.println("t1--->"+t1.getState());
System.out.println("t2--->"+t2.getState());
}
}
Upvotes: 2
Views: 1311
Reputation: 157
You have got the understanding correct, that every object has lock associated with it, and once Thread1 takes the lock to access synchronized method1, then Thread2 cannot access synchronized method2.
Now,coming to special case, you can always do a simple hack. Imagine Thread1 has the lock, and is accessing synchronized method1. Inside method1, if Thread1 calls wait on a variable, then it releases the lock on the object and goes to wait state. In this scenario, Thread2 can acquire the lock and enter synchronized method2. Hence, both the threads have access to different synchronized methods on same obj.
P.S. This is just for example, I would not recommend have another wait inside a synchronized block, it will lead to unnecessary complication. If any code has to do this, then it means there is flaw in logic and design.
Upvotes: 0
Reputation: 6158
You code is like the following:
void method1(){
synchronized(this) {
//...
}
}
synchronized void method2(){
synchronized(this) {
//...
}
}
And In your code, two threads can't enter two method at the same time for the lock contention. You can only do it on different instances of runnable i.e. different this.
Upvotes: 0
Reputation: 182829
Two threads cannot hold the same lock at the same time. However, just because a thread is inside a synchronized
method for an object, it does not follow that it holds the lock. For example, it could call await
on a condition variable and release the lock while it's waiting. That allows other threads to acquire the lock by entering synchronized
methods -- after all, that's what you're waiting for.
Upvotes: 0
Reputation: 1258
Notice that in your example threads are not competing on any thing even if MyRunnable1 class had a single method1() and your run() function was calling on this method in loop. Because thread1 and thread2 are different instances and each one has it's own copy of method1() so the locks used by synchronized are different and not one single lock.
Upvotes: 1