Source:
<code>public void onNext(final StartTime startTime) { LOG.log(Level.INFO, "StartTime: {0}", startTime); final EventHandler<AllocatedEvaluator> evalAllocHandlerForServer = getEvalAllocHandlerForServer(); final List<EventHandler<ActiveContext>> contextActiveHandlersForServer = new ArrayList<>(2); contextActiveHandlersForServer.add(getFirstContextActiveHandlerForServer(false)); contextActiveHandlersForServer.add(getSecondContextActiveHandlerForServer()); evaluatorManager.allocateEvaluators(initServerCount, evalAllocHandlerForServer, contextActiveHandlersForServer); final EventHandler<AllocatedEvaluator> evalAllocHandlerForWorker = getEvalAllocHandlerForWorker(); final List<EventHandler<ActiveContext>> contextActiveHandlersForWorker = new ArrayList<>(2); contextActiveHandlersForWorker.add(getFirstContextActiveHandlerForWorker(false)); contextActiveHandlersForWorker.add(getSecondContextActiveHandlerForWorker()); evaluatorManager.allocateEvaluators(dataLoadingService.getNumberOfPartitions(), evalAllocHandlerForWorker, contextActiveHandlersForWorker); final Identifier driverId = identifierFactory.getNewInstance(driverIdStr); workerEMWrapper.getNetworkSetup().registerConnectionFactory(driverId); serverEMWrapper.getNetworkSetup().registerConnectionFactory(driverId); psNetworkSetup.registerConnectionFactory(driverId); optimizerExecutor.submit(new Callable<Void>() { @Override public Void call() throws Exception { synchronizationManager.waitInitialization(); LOG.info("Worker tasks are initialized. Start triggering optimization."); <START> while (synchronizationManager.getCurrentState().equals(SynchronizationManager.STATE_RUN)) { <END> optimizationOrchestrator.run(); Thread.sleep(optimizationIntervalMs); } return null; } }); }</code><technical_language>encapsulate this condition SynchronizationManager.initialized() SynchronizationManager.allWorkersInitialized()? way, expose internal variable STATE_RUN here</technical_language>

Target:
public void onNext(final StartTime startTime) { LOG.log(Level.INFO, "StartTime: {0}", startTime); final EventHandler<AllocatedEvaluator> evalAllocHandlerForServer = getEvalAllocHandlerForServer(); final List<EventHandler<ActiveContext>> contextActiveHandlersForServer = new ArrayList<>(2); contextActiveHandlersForServer.add(getFirstContextActiveHandlerForServer(false)); contextActiveHandlersForServer.add(getSecondContextActiveHandlerForServer()); evaluatorManager.allocateEvaluators(initServerCount, evalAllocHandlerForServer, contextActiveHandlersForServer); final EventHandler<AllocatedEvaluator> evalAllocHandlerForWorker = getEvalAllocHandlerForWorker(); final List<EventHandler<ActiveContext>> contextActiveHandlersForWorker = new ArrayList<>(2); contextActiveHandlersForWorker.add(getFirstContextActiveHandlerForWorker(false)); contextActiveHandlersForWorker.add(getSecondContextActiveHandlerForWorker()); evaluatorManager.allocateEvaluators(dataLoadingService.getNumberOfPartitions(), evalAllocHandlerForWorker, contextActiveHandlersForWorker); final Identifier driverId = identifierFactory.getNewInstance(driverIdStr); workerEMWrapper.getNetworkSetup().registerConnectionFactory(driverId); serverEMWrapper.getNetworkSetup().registerConnectionFactory(driverId); psNetworkSetup.registerConnectionFactory(driverId); optimizerExecutor.execute(new Runnable() { @Override public void run() { while (synchronizationManager.workersInitializing()) { try { synchronizationManager.waitInitialization(); LOG.info("Worker tasks are initialized. Start triggering optimization."); } catch (final InterruptedException e) { LOG.log(Level.WARNING, "Interrupted while waiting for the worker initialization", e); } } while (!synchronizationManager.waitingCleanup()) { optimizationOrchestrator.run(); try { Thread.sleep(optimizationIntervalMs); } catch (final InterruptedException e) { LOG.log(Level.WARNING, "Interrupted while sleeping between optimizations", e); } } synchronizationManager.allowWorkersCleanup(); } }); }