Question

In JAVA : (a) Describe an application that can deadlock, or starve a thread. (b) Describe...

In JAVA :

(a) Describe an application that can deadlock, or starve a thread.

(b) Describe a testing strategy that would detect such a problem efficiently.

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

(a) Deadlock describes a situation in which two or more threads are indefinitely separated and await each other. A Java multithreaded program that suffers from a deadlock condition because the synchronized keyword triggers blocking of the executing thread while waiting for the lock or monitor associated with the defined entity.

Go through the java program below:


public class DemoThread {
       public static Object LockA = new Object();
       public static Object LockB = new Object();         
       public static void main(String args[]) {
       Thread1 t1 = new Thread1();
       Thread2 t2 = new Thread2();
       t1.start();
       t2.start();
       }         
       private static class Thread1 extends Thread {
       public void run() {
       synchronized (LockA) {
       System.out.println("t1- Holding lock 1");      
       try { Thread.sleep(10); }
       catch (InterruptedException o) {}
       System.out.println("t1- Waiting for lock 2");      
       synchronized (LockB) {
       System.out.println("t1- Holding lock 1 & 2");
       }
       }
       }
       }
       private static class Thread2 extends Thread {
       public void run() {
       synchronized (LockB) {
       System.out.println("t2- Holding lock 2");      
       try { Thread.sleep(10); }
       catch (InterruptedException o) {}
       System.out.println("t2- Waiting for lock 1");      
       synchronized (LockA) {
       System.out.println("t2- Holding lock 1 & 2");
       }
       }
       }
       }
       }

The Application above will linger indefinitely because none of the threads are in a position to continue and wait for each other to unlock the lock. You can terminate it by pressing Control + c.

Output:

t1- Holding lock 1
t2- Holding lock 2
t1- Waiting for lock 2
t2- Waiting for lock 1

(b) Programmatically, threads that have reached a deadlock state can be identified and the information about them can also be recovered. ThreadMXBean interface can be used to do this. This interface comes with java.lang.Management package.

First, you need to get an instance of ThreadMXBean using ManagementFactory's getThreadMXBean() method

ThreadMXBean bn = ManagementFactory.getThreadMXBean()

Then call findMonitorDeadlockedThreads() function on it after having received an instance of ThreadMXBean. It returns a list of type long that includes ids of all currently deadlocked threads.

Add a comment
Know the answer?
Add Answer to:
In JAVA : (a) Describe an application that can deadlock, or starve a thread. (b) Describe...
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
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
ADVERTISEMENT