package CHAP11;

public class C12_09_DeadlockExample {
    // 두 개의 공유 자원
    private static final Object resourceA = new Object();
    private static final Object resourceB = new Object();
    
    public static void main(String[] args) {
        // 첫 번째 스레드: resourceA → resourceB 순서로 점유
        Thread t1 = new Thread(() -> {
            synchronized (resourceA) {
                System.out.println("스레드 1: 자원 A 점유");
                try { Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (resourceB) {
                    System.out.println("스레드 1: 자원 B 점유");
                }
            }
        });

        // 두 번째 스레드: resourceB → resourceA 순서로 점유
        Thread t2 = new Thread(() -> {
            synchronized (resourceB) {
                System.out.println("스레드 2: 자원 B 점유");
                try { Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (resourceA) {
                    System.out.println("스레드 2: 자원 A 점유");
                }
            }
        });
        /*
        Thread t2 = new Thread(() -> {
            synchronized (resourceA) { // 순서 통일: resourceA → resourceB
                System.out.println("스레드 2: 자원 A 점유");
                try { Thread.sleep(100); } catch (InterruptedException e) {}
                synchronized (resourceB) {
                    System.out.println("스레드 2: 자원 B 점유");
                }
            }
        });
	   */
        t1.start();
        t2.start();
    }
}

