M2-T2 assignment YEAR-2nd BRANCH :- CSE SECTION :- Q BATCH :- 09 Reg No :- 231FA04D62 Data Base Management System
QUESTIONS:- a) Describe a situation in a Java application where three threads might encounter a deadlock while attempting to access shared resources, requiring locks on two different objects. Identify the conditions leading to deadlock, such as both threads holding a lock on one resource while waiting for the other, and being unable to release their locks. Write a Java program that illustrates this deadlock scenario, where Thread A locks Resource 1 and attempts to lock Resource 2, while Thread B locks Resource 2 and attempts to lock Resource 1and Resource3 and attempts to lock Resource1. Then, refactor the program to avoid deadlock by establishing a consistent order for locking resources and implementing timeout mechanisms for acquiring locks Finally, run the modified code to demonstrate that the deadlock has been resolved. b) Develop a Java application that demonstrates the effect of thread priorities on threadRequirementsscheduling. Create three threads with different priorities: MAX PRIORITY, NORM_PRIORITY, and MIN_PRIORITY. Each thread should execute a loop that runs 2000 iterations and print the current i teration number. 1. Write a class Priority Thread that extends Thread and implements the logic for running a loop with 2000 iterations. Each iteration should print the current iteration number. 2. In the main method, create three instances of Priority Thread, each assigned a different priority (MAX PRIORITY, NORM_PRIORITY, and MIN_PRIORITY). 3. Start all three threads simultaneously and use Thread sleep() to simulate some work done by each thread. 4. After all, threads complete, print out how many times each thread executed to observe the effect of priority on thread execution
AIM Problem Scenario:- Deadlock Scenario with Three Threads and Three Resources ThreadA locks Resource1 and attempts to lock Resource2. ThreadB lock Resource2 and attempts to lock Resource3. ThreadC locks Resource3 and attempts to lock Resource1. This creates a circular dependency: ThreadA waits for Resource2 held by ThreadB. ThreadB waits for Resource3 held by ThreadC. ThreadC waits for Resource1 held by ThreadA.
public class DeadlockDemo { private static final Object Resource1 = new Object(); private static final Object Resource2 = new Object(); private static final Object Resource3 = new Object(); public static void main(String[] args ) { Thread threadA = new Thread(() -> { synchronized (Resource1) { System.out.println ("Thread A: Locked Resource 1"); try { Thread.sleep (50); } catch ( InterruptedException ignored) {} System.out.println ("Thread A: Waiting for Resource 2"); synchronized (Resource2) { System.out.println ("Thread A: Locked Resource 2"); } } }); Thread threadB = new Thread(() -> { synchronized (Resource2) { System.out.println ("Thread B: Locked Resource 2"); SOURCE CODE:-
class PriorityThread extends Thread { public PriorityThread (String name, int priority) { super(name); setPriority (priority); } @Override public void run() { for (int i = 0; i < 2000; i ++) { System.out.println(getName() + " - Iteration: " + i ); try { Thread.sleep(1); // Simulate some work } catch (InterruptedException e) { Thread.currentThread ().interrupt(); e.printStackTrace (); } } } } public class ThreadPriorityDemo { public static void main(String[] args) { PriorityThread maxPriorityThread = new PriorityThread (" MaxPriorityThread ", SOURCE CODE:-
Thread.MAX_PRIORITY ); PriorityThread normPriorityThread = new PriorityThread (" NormPriorityThread ", Thread.NORM_PRIORITY ); PriorityThread minPriorityThread = new PriorityThread (" MinPriorityThread ", Thread.MIN_PRIORITY ); maxPriorityThread.start (); normPriorityThread.start (); minPriorityThread.start (); Thread additionalThread1 = new Thread(() -> { for (int i = 0; i < 2000; i ++) { System.out.println("Additional Thread 1 - Iteration: " + i ); } }); Thread additionalThread2 = new Thread(() -> { for (int i = 0; i < 2000; i ++) { System.out.println("Additional Thread 2 - Iteration: " + i ); } }); additionalThread1.start(); additionalThread2.start(); } } SOURCE CODE:-
OUTPUT:-
ALGORITHM:- Deadlock Scenario Algorithm two shared resources, Resource1 and Resource2. Create Thread A to lock Resource1, then attempt to lock Resource2. Create Thread B to lock Resource2, then attempt to lock Resource1. Start both threads, leading to a deadlock as each waits on the other’s lock. Avoiding Deadlock Algorithm Resource1 and Resource2 with reentrant locks and timeouts. Define Thread A to lock Resource1, then attempt to lock Resource2 with a timeout. Define Thread B to lock Resource2, then attempt to lock Resource1 with a timeout. Start both threads, avoiding deadlock as each can release locks and retry.