summaryrefslogtreecommitdiffstats
path: root/guava/src/com/google/common/util/concurrent/AbstractExecutionThreadService.java
diff options
context:
space:
mode:
authorYohann Roussel <yroussel@google.com>2014-03-19 16:25:37 +0100
committerYohann Roussel <yroussel@google.com>2014-03-20 15:13:33 +0100
commit4eceb95409e844fdc33c9c706e1dc307bfd40303 (patch)
treeee9f4f3fc79f757c79081c336bce4f1782c6ccd8 /guava/src/com/google/common/util/concurrent/AbstractExecutionThreadService.java
parent3d2402901b1a6462e2cf47a6fd09711f327961c3 (diff)
downloadtoolchain_jack-4eceb95409e844fdc33c9c706e1dc307bfd40303.zip
toolchain_jack-4eceb95409e844fdc33c9c706e1dc307bfd40303.tar.gz
toolchain_jack-4eceb95409e844fdc33c9c706e1dc307bfd40303.tar.bz2
Initial Jack import.
Change-Id: I953cf0a520195a7187d791b2885848ad0d5a9b43
Diffstat (limited to 'guava/src/com/google/common/util/concurrent/AbstractExecutionThreadService.java')
-rw-r--r--guava/src/com/google/common/util/concurrent/AbstractExecutionThreadService.java188
1 files changed, 188 insertions, 0 deletions
diff --git a/guava/src/com/google/common/util/concurrent/AbstractExecutionThreadService.java b/guava/src/com/google/common/util/concurrent/AbstractExecutionThreadService.java
new file mode 100644
index 0000000..6926c85
--- /dev/null
+++ b/guava/src/com/google/common/util/concurrent/AbstractExecutionThreadService.java
@@ -0,0 +1,188 @@
+/*
+ * Copyright (C) 2009 The Guava Authors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.google.common.util.concurrent;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Throwables;
+
+import java.util.concurrent.Executor;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Base class for services that can implement {@link #startUp}, {@link #run} and
+ * {@link #shutDown} methods. This class uses a single thread to execute the
+ * service; consider {@link AbstractService} if you would like to manage any
+ * threading manually.
+ *
+ * @author Jesse Wilson
+ * @since 1.0
+ */
+@Beta
+public abstract class AbstractExecutionThreadService implements Service {
+ private static final Logger logger = Logger.getLogger(
+ AbstractExecutionThreadService.class.getName());
+
+ /* use AbstractService for state management */
+ private final Service delegate = new AbstractService() {
+ @Override protected final void doStart() {
+ executor().execute(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ startUp();
+ notifyStarted();
+
+ if (isRunning()) {
+ try {
+ AbstractExecutionThreadService.this.run();
+ } catch (Throwable t) {
+ try {
+ shutDown();
+ } catch (Exception ignored) {
+ logger.log(Level.WARNING,
+ "Error while attempting to shut down the service"
+ + " after failure.", ignored);
+ }
+ throw t;
+ }
+ }
+
+ shutDown();
+ notifyStopped();
+ } catch (Throwable t) {
+ notifyFailed(t);
+ throw Throwables.propagate(t);
+ }
+ }
+ });
+ }
+
+ @Override protected void doStop() {
+ triggerShutdown();
+ }
+ };
+
+ /**
+ * Constructor for use by subclasses.
+ */
+ protected AbstractExecutionThreadService() {}
+
+ /**
+ * Start the service. This method is invoked on the execution thread.
+ *
+ * <p>By default this method does nothing.
+ */
+ protected void startUp() throws Exception {}
+
+ /**
+ * Run the service. This method is invoked on the execution thread.
+ * Implementations must respond to stop requests. You could poll for lifecycle
+ * changes in a work loop:
+ * <pre>
+ * public void run() {
+ * while ({@link #isRunning()}) {
+ * // perform a unit of work
+ * }
+ * }
+ * </pre>
+ * ...or you could respond to stop requests by implementing {@link
+ * #triggerShutdown()}, which should cause {@link #run()} to return.
+ */
+ protected abstract void run() throws Exception;
+
+ /**
+ * Stop the service. This method is invoked on the execution thread.
+ *
+ * <p>By default this method does nothing.
+ */
+ // TODO: consider supporting a TearDownTestCase-like API
+ protected void shutDown() throws Exception {}
+
+ /**
+ * Invoked to request the service to stop.
+ *
+ * <p>By default this method does nothing.
+ */
+ protected void triggerShutdown() {}
+
+ /**
+ * Returns the {@link Executor} that will be used to run this service.
+ * Subclasses may override this method to use a custom {@link Executor}, which
+ * may configure its worker thread with a specific name, thread group or
+ * priority. The returned executor's {@link Executor#execute(Runnable)
+ * execute()} method is called when this service is started, and should return
+ * promptly.
+ *
+ * <p>The default implementation returns a new {@link Executor} that sets the
+ * name of its threads to the string returned by {@link #getServiceName}
+ */
+ protected Executor executor() {
+ return new Executor() {
+ @Override
+ public void execute(Runnable command) {
+ new Thread(command, getServiceName()).start();
+ }
+ };
+ }
+
+ @Override public String toString() {
+ return getServiceName() + " [" + state() + "]";
+ }
+
+ // We override instead of using ForwardingService so that these can be final.
+
+ @Override public final ListenableFuture<State> start() {
+ return delegate.start();
+ }
+
+ @Override public final State startAndWait() {
+ return delegate.startAndWait();
+ }
+
+ @Override public final boolean isRunning() {
+ return delegate.isRunning();
+ }
+
+ @Override public final State state() {
+ return delegate.state();
+ }
+
+ @Override public final ListenableFuture<State> stop() {
+ return delegate.stop();
+ }
+
+ @Override public final State stopAndWait() {
+ return delegate.stopAndWait();
+ }
+
+ @Override public final void addListener(Listener listener, Executor executor) {
+ delegate.addListener(listener, executor);
+ }
+
+ /**
+ * Returns the name of this service. {@link AbstractExecutionThreadService}
+ * may include the name in debugging output.
+ *
+ * <p>Subclasses may override this method.
+ *
+ * @since 10.0
+ */
+ protected String getServiceName() {
+ return getClass().getSimpleName();
+ }
+}