Question: public class ConcurrencyManagementExample { private static final Object lock 1 = new Object ( ) ; private static final Object lock 2 = new Object

public class ConcurrencyManagementExample {
private static final Object lock1= new Object();
private static final Object lock2= new Object();
private static final Object lock3= new Object();
public static void main(String[] args){
Thread thread1= new Thread(()->{
synchronized (lock1){
System.out.println("Thread 1 acquired lock1");
try {
Thread.sleep(100);
} catch (InterruptedException e){
e.printStackTrace();
}
synchronized (lock2){
System.out.println("Thread 1 acquired lock2");
// Critical section
}
}
});
Thread thread2= new Thread(()->{
synchronized (lock2){
System.out.println("Thread 2 acquired lock2");
try {
Thread.sleep(100);
} catch (InterruptedException e){
e.printStackTrace();
}
synchronized (lock3){
System.out.println("Thread 2 acquired lock3");
// Critical section
}
}
});
thread1.start();
thread2.start();
}
}
What is the most likely output from running above program?
The screen show the following println() lines:
Thread 1 acquired lock1;
Thread 1 acquired lock2;
Thread 2 acquired lock2;
Thread 2 acquired lock3;
The screen show the following println() lines:
Thread 1 acquired lock1;
Thread 1 acquired lock2;
Thread 2 acquired lock2;
Thread 2 acquired lock3;
or
Thread 2 acquired lock2;
Thread 2 acquired lock3;
Thread 1 acquired lock1;
Thread 1 acquired lock2;
deadlock or livelock
The 1st thread is starved because the 2nd thread holds the lock2 and is sleeping, thus delaying thread1 from acquiring lock2.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!