In: Computer Science
Java programming:
Save the program as DeadlockExample.java
Run the program below. Note whether deadlock occurs. Then modify the program to add two more threads: add a class C and a class D, and call them from main. Does deadlock occur?
import java.util.concurrent.locks.*;
class A implements Runnable
{
private Lock first, second;
public A(Lock first, Lock second) {
this.first = first;
this.second = second;
}
public void run() {
try {
first.lock();
System.out.println("Thread A got first lock.");
// do something
try {
Thread.sleep( ((int)(3*Math.random()))*1000);
}
catch (InterruptedException e) {}
second.lock();
System.out.println("Thread A got second lock.");
// do something
}
finally {
first.unlock();
second.unlock();
}
}
}
class B implements Runnable
{
private Lock first, second;
public B(Lock first, Lock second) {
this.first = first;
this.second = second;
}
public void run() {
try {
second.lock();
System.out.println("Thread B got second lock.");
// do something
try {
Thread.sleep( ((int)(3*Math.random()))*1000);
}
catch (InterruptedException e) {}
first.lock();
System.out.println("Thread B got first lock.");
// do something
}
finally {
second.unlock();
first.unlock();
}
}
}
public class DeadlockExample
{
public static void main(String arg[]) {
Lock lockX = new ReentrantLock();
Lock lockY = new ReentrantLock();
Thread threadA = new Thread(new A(lockX,lockY));
Thread threadB = new Thread(new B(lockX,lockY));
threadA.start();
threadB.start();
System.out.println("Deadlock!!!!");
}
}
Yes deadlock occurs but with with a warning that max real time limit exceeded as the output below shows:
Code:
import java.util.concurrent.locks.*;
import java.io.*;
class A implements Runnable
{
private Lock first, second, third1, fourth1;
public A(Lock first, Lock second, Lock third1, Lock fourth1) {
this.first = first;
this.second = second;
this.third1 = third1;
this.fourth1 = fourth1;
}
public void run() {
try {
first.lock();
System.out.println("Thread A got first lock.");
// do something
try {
Thread.sleep( ((int)(3*Math.random()))*1000);
}
catch (InterruptedException e) {}
second.lock();
third1.lock();
fourth1.lock();
System.out.println("Thread A got second lock.");
System.out.println("Thread A got third lock.");
System.out.println("Thread A got fourth lock.");
// do something
}
finally {
first.unlock();
second.unlock();
third1.unlock();
fourth1.unlock();
}
}
}
class B implements Runnable
{
private Lock first, second, third1, fourth1;
public B(Lock first, Lock second, Lock third1, Lock fourth1) {
this.first = first;
this.second = second;
this.third1 = third1;
this.fourth1 = fourth1;
}
public void run() {
try {
second.lock();
System.out.println("Thread B got second lock.");
// do something
try {
Thread.sleep( ((int)(3*Math.random()))*1000);
}
catch (InterruptedException e) {}
first.lock();
third1.lock();
fourth1.lock();
System.out.println("Thread B got first lock.");
System.out.println("Thread B got third lock.");
System.out.println("Thread B got fourth lock.");
// do something
}
finally {
second.unlock();
first.unlock();
third1.unlock();
fourth1.unlock();
}
}
}
class C implements Runnable
{
private Lock first, second, third1, fourth1;
public C(Lock first, Lock second, Lock third1, Lock fourth1) {
this.first = first;
this.second = second;
this.third1 = third1;
this.fourth1 = fourth1;
}
public void run() {
try {
third1.lock();
System.out.println("Thread C got third lock.");
// do something
try {
Thread.sleep( ((int)(3*Math.random()))*1000);
}
catch (InterruptedException e) {}
first.lock();
second.lock();
fourth1.lock();
System.out.println("Thread A got first lock.");
System.out.println("Thread A got second lock.");
System.out.println("Thread A got fourth lock.");
// do something
}
finally {
third1.unlock();
first.unlock();
second.unlock();
fourth1.unlock();
}
}
}
class D implements Runnable
{
private Lock first, second, third1, fourth1;
public D(Lock first, Lock second, Lock third1, Lock fourth1) {
this.first = first;
this.second = second;
this.third1 = third1;
this.fourth1 = fourth1;
}
public void run() {
try {
fourth1.lock();
System.out.println("Thread C got fourth lock.");
// do something
try {
Thread.sleep( ((int)(3*Math.random()))*1000);
}
catch (InterruptedException e) {}
first.lock();
second.lock();
third1.lock();
System.out.println("Thread A got first lock.");
System.out.println("Thread A got second lock.");
System.out.println("Thread A got third lock.");
// do something
}
finally {
fourth1.unlock();
first.unlock();
second.unlock();
third1.unlock();
}
}
}
public class Main
{
public static void main(String arg[]) {
Lock lockA = new ReentrantLock();
Lock lockB = new ReentrantLock();
Lock lockC = new ReentrantLock();
Lock lockD = new ReentrantLock();
Thread threadA = new Thread(new A(lockA,lockB, lockC, lockD));
Thread threadB = new Thread(new B(lockA,lockB, lockC, lockD));
Thread threadC = new Thread(new C(lockA,lockB, lockC, lockD));
Thread threadD = new Thread(new D(lockA,lockB, lockC, lockD));
threadA.start();
threadB.start();
threadC.start();
threadD.start();
System.out.println("Deadlock!!!!");
}
}
Yes, deadlock occurs!