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
  • which of the following can occur when the only thread of a process (a process with...

    which of the following can occur when the only thread of a process (a process with a single thread) tries to acquire a mutex (take a lock)? a. lock contention b. lock overhead c. deadlock d. data race

  • 7- For each of the following systems, which deadlock strategy would you lmplement? Justity a. OS...

    7- For each of the following systems, which deadlock strategy would you lmplement? Justity a. OS running on a student laptop (1 pt) b. OS in a self-driving car (1 pt) c. Computer that can afford a deadlock for no more than 5 minutes (2 pts)

  • Hello, Can someone help e with this Java problem. Please ? One interesting application of computers...

    Hello, Can someone help e with this Java problem. Please ? One interesting application of computers is to display graphs and bar charts. Write an application that reads five numbers, between 1 and 30. For each number that’s read, your program should display the same number of adjacent asterisks. For example, if your program reads the number 7, it should display *******. Display the bars of asterisks after you read all five numbers. Show a few running results in the...

  • If an auditor decides to assess control risk as low based on IT application control procedures,...

    If an auditor decides to assess control risk as low based on IT application control procedures, which of the following would not be part of the auditor’s strategy for testing controls? a. Testing the effectiveness of management review controls used to monitor the results of operations. b. Testing the effectiveness of manual follow-up procedures. c. Testing the effectiveness of the application with test data. d. Testing the effectiveness of IT general control procedures.

  • TRUE-FALSE     Basic synchronization principles and multithreading 1. Java user threads can implement both busy-waiting and no-busy-waiting...

    TRUE-FALSE     Basic synchronization principles and multithreading 1. Java user threads can implement both busy-waiting and no-busy-waiting policy. 2. Priority inversion avoids deadlocks. 3. Spinlock mutex can be used as an adaptive mutex. 4. Java RTE can be blocked for Input/Output operation. 5. Interrupted user thread, which executes a method in a monitor, must be rolled back to undo any changes it performed. 6. The synchronization primitive by disabling interrupts can be used by an application program. 7. Bounded-waiting requirement is...

  • Write a java application that should have a menu system along the following lines (you can...

    Write a java application that should have a menu system along the following lines (you can have sub-menus if you deem that to be necessary) 1) Ask the user how many books their application should store • The application then creates a suitable storage component for the books (i.e. an array of Book objects) 2) Add book details • You as the reader can add details of a book here (i.e. a Book object’s properties are given values). 3) Display...

  • java c++ 1. Write a method called deleteFromList in an application (not in the class LinkedList)...

    java c++ 1. Write a method called deleteFromList in an application (not in the class LinkedList) with the header public static void deleteFromList (LinkedList LL, LinkedList Current) to delete all values found in linked list LL from another linked list Current. The figure below demonstrates the concept List LL 55 45 Current List 77 55 50 55 2 Current after deletion 77 50 2 2. There are many ways to detect if a phrase is a palindrome. You already learned...

  • 6. Lignin can be used in adhesives but the real application has not been very successful. Please describe the reason an...

    6. Lignin can be used in adhesives but the real application has not been very successful. Please describe the reason and methods to solve this problem (10 pts).

  • Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design...

    Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design and code a Java console application that reads in seven integer values and prints out the number of occurrences of each value. The application uses the Java single dimension array construct to implement its functionality. Your program output should look like the sample output provided in the "Count Occurrences in Seven Integers Using Java Single Dimension Arrays Instructions" course file resource. Full instructions for...

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