[BAEL-4500] Add locked ownable synchronizers example code (#12608)

* Add locked ownable synchronizers example code

* refactor application snippet
This commit is contained in:
kpentaris 2022-08-24 16:47:46 +03:00 committed by GitHub
parent 923ab8e513
commit 876d78b188
1 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package com.baeldung.ownablesynchronizers;
import java.util.concurrent.locks.ReentrantLock;
public class Application {
public static Thread createThread(ReentrantLock mainLock, ReentrantLock otherLock, String threadName) {
return new Thread(() -> {
try {
mainLock.lock();
// synchronized block here allows us to wait for other thread to lock,
// in order to simulate a deadlock
synchronized (Application.class) {
Application.class.notify();
if (!otherLock.isLocked()) {
Application.class.wait();
}
}
// thread will hang here
otherLock.lock();
System.out.println(threadName + ": Finished");
mainLock.unlock();
otherLock.unlock();
} catch (InterruptedException e) {
System.out.println(threadName + ": " + e.getMessage());
}
});
}
public static void main(String[] args) throws Exception {
ReentrantLock firstLock = new ReentrantLock(true);
ReentrantLock secondLock = new ReentrantLock(true);
Thread first = createThread(firstLock, secondLock, "Thread-0");
Thread second = createThread(secondLock, firstLock, "Thread-1");
first.start();
second.start();
first.join();
second.join();
}
}