Untitled

mail@pastecode.io avatar
unknown
java
2 months ago
932 B
2
Indexable
Never
public class DeadlockApp {
    private static final Object resource1 = new Object();
    private static final Object resource2 = new Object();
    private static boolean deadlockEnabled = true;

    public static void main(String[] args) {
        while (true) {
            synchronized (resource1) {
                System.out.println(Thread.currentThread().getName() + " acquired resource1");
                try {
                    Thread.sleep(100); // имитация работы
                } catch (InterruptedExcption e) {
                    e.printStackTrace();
                }
                // проверка дедлока
                if (deadlockEnabled) {
                    synchronized (resource2) {
                        System.out.println(Thread.currentThread().getName() + " acquired resource2");
                    }
                }
            }
        }
    }
}
Leave a Comment