Question

Java programming

Exercise 32.11 Write a program that uses explicit locks (or semaphores) to cause a deadlock. Do not use the synchronized keyw

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DeadlockUsingSemaphore {
  
final static Lock lock1 = new ReentrantLock();
final static Lock lock2 = new ReentrantLock();
  
public static void main(String[]args)
{
Thread t1 = new Thread(new Runnable3());
t1.setName("First Thread");
t1.start();
  
Thread t2 = new Thread(new Runnable4());
t2.setName("Second Thread");
t2.start();
}
  
static class Runnable3 implements Runnable
{
public void run()
{
DeadlockUsingSemaphore.lock1.lock();
try
{
System.out.println(Thread.currentThread().getName() + ": acquired lock1. Trying for lock2...");
try
{
Thread.sleep(500);
}catch(InterruptedException e){ e.printStackTrace(); }
  
DeadlockUsingSemaphore.lock2.lock();
try
{
System.out.println(Thread.currentThread().getName() + ": acquired lock2.");
} finally {
DeadlockUsingSemaphore.lock2.unlock();
}
}finally{
DeadlockUsingSemaphore.lock1.lock();
}
}
}
  
static class Runnable4 implements Runnable
{
public void run()
{
DeadlockUsingSemaphore.lock2.lock();
try
{
System.out.println(Thread.currentThread().getName() + ": acquired lock2. Trying for lock1...");
try
{
Thread.sleep(500);
}catch(InterruptedException e){ e.printStackTrace(); }
DeadlockUsingSemaphore.lock1.lock();
try
{
System.out.println(Thread.currentThread().getName() + ": acquired lock1.");
}finally{
DeadlockUsingSemaphore.lock1.unlock();
}
}finally{
DeadlockUsingSemaphore.lock2.unlock();
}
}
}
}

****************************************************************** SCREENSHOT *********************************************************

run: First Thread: acquired lock1. Trying for lock2... Second Thread: acquired lock2. Trying for lock1.

Add a comment
Know the answer?
Add Answer to:
Java programming Exercise 32.11 Write a program that uses explicit locks (or semaphores) to cause a deadlock. Do not us...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Prelab Exercises Your task is to write a Java program that will print out the following...

    Prelab Exercises Your task is to write a Java program that will print out the following message (including the row of equal marks): Computer Science, Yes!!!! ========================= An outline of the program is below. Complete it as follows: a. In the documentation at the top, fill in the name of the file the program would be saved in and a brief description of what the program does. b. Add the code for the main method to do the printing. //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT