private static void lockAndExecute(final Runnable runnable, final Iterator<Guardian> guardians) { if (guardians.hasNext()) { final Guardian guardian = guardians.next(); ThreadUtils.log("Acquire lock: %s", guardian); synchronized (guardian) { ThreadUtils.log("Lock: %s acquired", guardian); Guardian.lockAndExecute(runnable, guardians); } ThreadUtils.log("Release lock: %s", guardian); } else { ThreadUtils.log("All locks are acquired. Execute task"); runnable.run(); } }
public static void main(final String[] args) throws Exception { // Create a lock and acquire it final Lock lock = new ReentrantLock(); lock.lock(); try { // Create another thread that tries to acquire the same lock final Thread thread = new Thread( new Runnable() { @Override public void run() { try { // Wait a minute before it gives up ThreadUtils.log("Acquiring the lock..."); if (lock.tryLock(1, TimeUnit.MINUTES)) { try { ThreadUtils.log("Working..."); } finally { lock.unlock(); } } } catch (final InterruptedException e) { Thread.currentThread().interrupt(); ThreadUtils.log("Cancelled"); } } }, "Thread-1"); thread.start(); // Wait 2 seconds before interrupting the thread Thread.sleep(2000); // Cancel the thread from what it is doing and wait for it to finish thread.interrupt(); thread.join(); } finally { lock.unlock(); } ThreadUtils.log("Done"); }