1 /* 2 * Copyright 2017 Google LLC 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google LLC nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package com.google.api.gax.core; 31 32 import java.util.concurrent.TimeUnit; 33 34 /** 35 * Represents a resource running in the background that needs to be shut down for resources to be 36 * released. 37 */ 38 public interface BackgroundResource extends AutoCloseable { 39 40 /** 41 * Initiates an orderly shutdown in which previously submitted work is finished, but no new work 42 * will be accepted. Invocation has no additional effect if already shut down. 43 * 44 * <p>This method does not wait for previously submitted work to complete execution. Use 45 * awaitTermination to do that. 46 */ shutdown()47 void shutdown(); 48 49 /** Returns true if this background resource has been shut down. */ isShutdown()50 boolean isShutdown(); 51 52 /** 53 * Returns true if all work has completed following shut down. Note that isTerminated is never 54 * true unless either shutdown or shutdownNow was called first. 55 */ isTerminated()56 boolean isTerminated(); 57 58 /** 59 * Attempts to stop all actively executing work and halts the processing of waiting work. 60 * 61 * <p>This method does not wait for actively executing work to terminate. Use awaitTermination to 62 * do that. 63 * 64 * <p>There are no guarantees beyond best-effort attempts to stop processing actively executing 65 * work. For example, typical implementations will cancel via Thread.interrupt(), so any task that 66 * fails to respond to interrupts may never terminate. 67 */ shutdownNow()68 void shutdownNow(); 69 70 /** 71 * Blocks until all work has completed execution after a shutdown request, or the timeout occurs, 72 * or the current thread is interrupted, whichever happens first. 73 */ awaitTermination(long duration, TimeUnit unit)74 boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException; 75 76 // NEXT_MAJOR_VER: override close() to remove 'throws Exception' 77 } 78