public static CommandExecutor.Method getFreeExecutor(Stage st) { while (true) { synchronized (executorsMap) { try { for (Map.Entry<CommandExecutor.Method, Stage> entry : executorsMap.entrySet()) { if (!entry.getKey().isInUse() && entry.getValue() == st) { CommandExecutor.Method m = entry.getKey(); m.setInUse(true); return m; } } LOG.info("All executors for stage " + st + " in use (will wait..)"); executorsMap.wait(); } catch (InterruptedException ie) { return null; } } } }