Search in sources :

Example 6 with State

use of com.google.common.util.concurrent.Service.State in project guava by google.

the class AbstractServiceTest method testListenerDoesntDeadlockOnStopAndWaitFromTerminated.

public void testListenerDoesntDeadlockOnStopAndWaitFromTerminated() throws Exception {
    final NoOpThreadedService service = new NoOpThreadedService();
    service.addListener(new Listener() {

        @Override
        public void terminated(State from) {
            service.stopAsync().awaitTerminated();
        }
    }, directExecutor());
    service.startAsync().awaitRunning();
    Thread thread = new Thread() {

        @Override
        public void run() {
            service.stopAsync().awaitTerminated();
        }
    };
    thread.start();
    thread.join(LONG_TIMEOUT_MILLIS);
    assertFalse(thread + " is deadlocked", thread.isAlive());
}
Also used : Listener(com.google.common.util.concurrent.Service.Listener) State(com.google.common.util.concurrent.Service.State) Thread.currentThread(java.lang.Thread.currentThread)

Example 7 with State

use of com.google.common.util.concurrent.Service.State in project guava by google.

the class AbstractService method stopAsync.

@CanIgnoreReturnValue
@Override
public final Service stopAsync() {
    if (monitor.enterIf(isStoppable)) {
        try {
            State previous = state();
            switch(previous) {
                case NEW:
                    snapshot = new StateSnapshot(TERMINATED);
                    enqueueTerminatedEvent(NEW);
                    break;
                case STARTING:
                    snapshot = new StateSnapshot(STARTING, true, null);
                    enqueueStoppingEvent(STARTING);
                    break;
                case RUNNING:
                    snapshot = new StateSnapshot(STOPPING);
                    enqueueStoppingEvent(RUNNING);
                    doStop();
                    break;
                case STOPPING:
                case TERMINATED:
                case FAILED:
                    // These cases are impossible due to the if statement above.
                    throw new AssertionError("isStoppable is incorrectly implemented, saw: " + previous);
                default:
                    throw new AssertionError("Unexpected state: " + previous);
            }
        } catch (Throwable shutdownFailure) {
            notifyFailed(shutdownFailure);
        } finally {
            monitor.leave();
            dispatchListenerEvents();
        }
    }
    return this;
}
Also used : Preconditions.checkState(com.google.common.base.Preconditions.checkState) State(com.google.common.util.concurrent.Service.State) CanIgnoreReturnValue(com.google.errorprone.annotations.CanIgnoreReturnValue)

Aggregations

State (com.google.common.util.concurrent.Service.State)7 Preconditions.checkState (com.google.common.base.Preconditions.checkState)4 Listener (com.google.common.util.concurrent.Service.Listener)2 CanIgnoreReturnValue (com.google.errorprone.annotations.CanIgnoreReturnValue)2 Thread.currentThread (java.lang.Thread.currentThread)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1