@Test public void testT1StartT2Start() throws Exception { ExecutorService pool = Executors.newCachedThreadPool(); final CyclicBarrier barrier = new CyclicBarrier(2); final AtomicInteger start = new AtomicInteger(0); final AbstractServer server = new AbstractServer() { protected void doStart() { logger.finer("Server " + this + " in doStart"); if (start.compareAndSet(0, 1)) { await(barrier); await(barrier); } } protected void doStop() { logger.finer("Server " + this + " in doStop"); } }; Callable<Boolean> starter = new Callable<Boolean>() { public Boolean call() throws Exception { return server.start(); } }; Future<Boolean> t1 = pool.submit(starter); barrier.await(); barrier1: {} Future<Boolean> t2 = pool.submit(starter); assert !t2.get(); barrier.await(); barrier2: {} assert t1.get(); }
public boolean start() { if (!status.compareAndSet(Status.READY, Status.STARTING)) { if (logger.isLoggable(Level.FINER)) logger.finer("Server " + this + " is not ready"); return false; } if (logger.isLoggable(Level.FINER)) logger.finer("Server " + this + " starting..."); doStart(); status.set(Status.RUNNING); if (logger.isLoggable(Level.FINE)) logger.fine("Server " + this + " started successfully"); return true; }
AbstractServer implements Server { public boolean start() { if (!status.compareAndSet(Status.READY, Status.STARTING)) { if (logger.isLoggable(Level.FINER)) logger.finer("Server " + this + " is not ready"); return false; } if (logger.isLoggable(Level.FINER)) logger.finer("Server " + this + " starting..."); doStart(); status.set(Status.RUNNING); if (logger.isLoggable(Level.FINE)) logger.fine("Server " + this + " started successfully"); return true; } }
AbstractServer implements Server { public boolean start() { if (!status.compareAndSet(Status.READY, Status.STARTING)) { if (logger.isLoggable(Level.FINER)) logger.finer("Server " + this + " is not ready"); return false; } if (logger.isLoggable(Level.FINER)) logger.finer("Server " + this + " starting..."); doStart(); status.set(Status.RUNNING); if (logger.isLoggable(Level.FINE)) logger.fine("Server " + this + " started successfully"); return true; }  }
AbstractServer implements Server { public boolean start() { if (!status.compareAndSet(Status.READY, Status.STARTING)) { if (logger.isLoggable(Level.FINER)) logger.finer("Server " + this + " is not ready"); return false; } if (logger.isLoggable(Level.FINER)) logger.finer("Server " + this + " starting..."); doStart(); status.set(Status.RUNNING); if (logger.isLoggable(Level.FINE)) logger.fine("Server " + this + " started successfully"); return true; }  boolean isRunning(); boolean start(); boolean stop(); }
AbstractServer implements Server { public boolean start() { if (!status.compareAndSet(Status.READY, Status.STARTING)) { if (logger.isLoggable(Level.FINER)) logger.finer("Server " + this + " is not ready"); return false; } if (logger.isLoggable(Level.FINER)) logger.finer("Server " + this + " starting..."); doStart(); status.set(Status.RUNNING); if (logger.isLoggable(Level.FINE)) logger.fine("Server " + this + " started successfully"); return true; }  boolean isRunning(); boolean start(); boolean stop();  }