Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Deadlock occurs when no processing can occur because two processes that are waiting for each other to finish. For example, imagine that two processes need

Deadlock occurs when no processing can occur because two processes that are waiting for each other to finish. For example, imagine that two processes need access to a file or database table row in order to complete, but both processes are attempting to access that resource at the same time. Neither process can complete without the other releasing access to the required resource, so the result is deadlock.

Read and analyze code in the linked document that spawns two different threads at the same time.

1. Predict the results of executing the program.

2. Identify whether a deadlock or starvation event will occur and, if so, at what point in the code.

Here is the code:

Analyzing a Multithreaded Program

/**********************************************************************

* Program: Week 4 Analyze a Multithreaded Program

* Purpose: Review the code and predict the results for the program execution.

* Programmer: Iam A. student

* Instructor: xxx

***********************************************************************/

Package example deadlk;

public class Deadlock {

public static Object Lock1 = new Object(); // aacquires lock on the Lock1 object

public static Object Lock2 = new Object(); // aacquires lock on the Lock2 object

public static void main(String args[]) {

ThreadDemo1 T1 = new ThreadDemo1(); // define the new threads

ThreadDemo2 T2 = new ThreadDemo2(); // and set name for the threads

T1.start();

T2.start();

}

private static class ThreadDemo1 extends Thread {

public void run() {

synchronized (Lock1) {

// synchronized Lock1 and Lock 2 will hold the thread until release

System.out.println("Thread 1: Holding lock 1...");

try { Thread.sleep(10); } // pause for 10 secs, then access thread

catch (InterruptedException e) {} // if exception happens, catch it

System.out.println("Thread 1: Waiting for lock 2..."); // display wait condition

synchronized (Lock2) {

System.out.println("Thread 1: Holding lock 1 & 2...");

}

}

}

}

private static class ThreadDemo2 extends Thread {

public void run() {

synchronized (Lock2) {

System.out.println("Thread 2: Holding lock 2...");

try { Thread.sleep(10); } // pause for 10 secs, then access thread

catch (InterruptedException e) {} // if exception happens, catch it

System.out.println("Thread 2: Waiting for lock 1..."); // display wait condition

synchronized (Lock1) {

System.out.println("Thread 2: Holding lock 1 & 2...");

}

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

More Books

Students also viewed these Databases questions