I just purposely came up with such a demonstration, but in fact I have no idea where exactly it can be used. Maybe this demo might help a bit :)
private static void testReentrantLock() { ReentrantLock lock = new ReentrantLock(); Thread thread = new Thread(() -> { int i = 0; System.out.println("before entering ReentrankLock block"); try { lock.lockInterruptibly(); while (0 < 1) { System.out.println("in the ReentrankLock block counting: " + i++); } } catch (InterruptedException e) { System.out.println("ReentrankLock block interrupted"); } }); lock.lock(); thread.start(); thread.interrupt(); }
Exit
before entering ReentrankLock block ReentrankLock block interrupted
Hearen
source share