Reputation: 703
Can some one explain me what does object reference expression is meant over herein synchronized block?
synchronized (object reference expression) {
//code block
}
public class DeadlockExample {
public static void main(String[] args) {
final String resource1 = "ratan jaiswal";
final String resource2 = "vimal jaiswal";
// t1 tries to lock resource1 then resource2
Thread t1 = new Thread() {
public void run() {
synchronized (resource1) {
System.out.println("Thread 1: locked resource 1");
try { Thread.sleep(100);} catch (Exception e) {}
synchronized (resource2) {
System.out.println("Thread 1: locked resource 2");
}
}
}
};
// t2 tries to lock resource2 then resource1
Thread t2 = new Thread() {
public void run() {
synchronized (resource2) {
System.out.println("Thread 2: locked resource 2");
try { Thread.sleep(100);} catch (Exception e) {}
synchronized (resource1) {
System.out.println("Thread 2: locked resource 1");
}
}
}
};
t1.start();
t2.start();
}
}
like over here what does resource1 in synchronized block is doing here?
Upvotes: 0
Views: 867
Reputation: 153
Synchronisation should be used on an object or methods not on variables. You have used it in wrong way. Synchronisation on object means, you will be having a lock over the object. No other threads can access. Once the processing is done, lock is released.
Upvotes: 0
Reputation: 36304
You are writing a program to simulate a deadlock.. how it works?
public class DeadlockExample {
public static void main(String[] args) {
final String resource1 = "ratan jaiswal";
final String resource2 = "vimal jaiswal";
// t1 tries to lock resource1 then resource2
Thread t1 = new Thread() {
public void run() {
synchronized (resource1) { //try to get lock on String resource1, no toher thread can access resource1 until this synchronized block ends (provided you've indeed entered this block by acquiring the lock..)
System.out.println("Thread 1: locked resource 1");
try { Thread.sleep(100);} catch (Exception e) {} //sleep() doesn't release the lock
synchronized (resource2) { //try to get lock on string get lock on String resource2
System.out.println("Thread 1: locked resource 2");
}
}
}
};
// t2 tries to lock resource2 then resource1
Thread t2 = new Thread() {
public void run() {
synchronized (resource2) { //try to get lock on String resource2
System.out.println("Thread 2: locked resource 2");
try { Thread.sleep(100);} catch (Exception e) {} //sleep() doesn't release the lock
synchronized (resource1) { //try to get lock on String resource1
System.out.println("Thread 2: locked resource 1");
}
}
}
};
t1.start();
t2.start();
}
}
Upvotes: 1
Reputation: 359
In short, objects can be used as mutual exclusion locks. Given a single object (such as resource1), no more than one thread can be inside a synchronized block over the same instance of the resource. The other waiting threads would wait until the first thread exits the block.
This object has various methods for this synchronization such as "wait" and "notify", used to further extend the synchronization support. A thread inside a synchronization block on an object may call "wait" to release the lock and wait until another thread calls "notify" or "notifyAll" on that same object. When that thread then wakes up, it will try to re-acquire the lock represented by this object. This is useful for writing condition variables / monitors.
Upvotes: 3