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 lockAndExecute(final Runnable runnable, final List<Guardian> guardians) { final List<Guardian> orderedGuardians = new ArrayList<>(guardians); Collections.sort(orderedGuardians); Guardian.lockAndExecute(runnable, orderedGuardians.iterator()); }
public static void lockAndExecute(final Runnable runnable, final Guardian... guardians) { Guardian.lockAndExecute(runnable, Arrays.asList(guardians)); }