Skip to content

Codes #177

@siddharthrgade21-a11y

Description

@siddharthrgade21-a11y

public class DeadlockExample {
public static Object Lock1 = new Object();
public static Object Lock2 = new Object();

public static void main(String[] args) {
    ThreadDemo1 T1 = new ThreadDemo1();
    ThreadDemo2 T2 = new ThreadDemo2();
    T1.start();
    T2.start();
}

private static class ThreadDemo1 extends Thread {
    public void run() {
        synchronized (Lock1) {
            System.out.println("Thread 1: Holding lock 1...");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            System.out.println("Thread 1: Waiting for lock 2...");
            synchronized (Lock2) {
                System.out.println("Thread 1: Holding lock 1 & 2...");
            }
        }
    }
}

private static class ThreadDemo2 extends Thread {
    public void run() {
        synchronized (Lock2) { // Changed from Lock1 to Lock2 to create deadlock
            System.out.println("Thread 2: Holding lock 2...");
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
            System.out.println("Thread 2: Waiting for lock 1...");
            synchronized (Lock1) { // Changed from Lock2 to Lock1 to create deadlock
                System.out.println("Thread 2: Holding lock 2 & 1...");
            }
        }
    }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions