Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package multithreading.lock.reentrant_custom_lock_fail;
- import multithreading.lock.custom_lock.CustomLock;
- import org.checkerframework.checker.units.qual.C;
- public class ReentrantCustomLockFailExample {
- private final CustomLock lock = new CustomLock();
- public synchronized void outer() throws InterruptedException {
- lock.lock();
- System.out.println(Thread.currentThread().getName() + " enters OUTER");
- Thread.sleep(500);
- inner();
- lock.unlock();
- }
- private synchronized void inner() throws InterruptedException {
- lock.lock();
- System.out.println(Thread.currentThread().getName() + " tries to enter INNER");
- System.out.println(Thread.currentThread().getName() + " enters INNER");
- lock.unlock();
- }
- }
- package multithreading.lock.reentrant_custom_lock_fail;
- public class ReentrantCustomLockFailExampleClient {
- public static void main(String[] args) throws InterruptedException {
- ReentrantCustomLockFailExample reentrantCustomLockFailExample = new ReentrantCustomLockFailExample();
- for (int i = 0; i < 5; i++) {
- new Thread(() -> {
- try {
- reentrantCustomLockFailExample.outer();
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }, "Thread " + i).start();
- }
- Thread.sleep(7000); // needed to wait all threads to be completed (I'm lazy to call join on them after starting =) )
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement