rt/emul/compact/src/main/java/java/util/concurrent/ScheduledExecutorService.java
branchjdk7-b147
changeset 1890 212417b74b72
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/util/concurrent/ScheduledExecutorService.java	Sat Mar 19 10:46:31 2016 +0100
     1.3 @@ -0,0 +1,187 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.6 + *
     1.7 + * This code is free software; you can redistribute it and/or modify it
     1.8 + * under the terms of the GNU General Public License version 2 only, as
     1.9 + * published by the Free Software Foundation.  Oracle designates this
    1.10 + * particular file as subject to the "Classpath" exception as provided
    1.11 + * by Oracle in the LICENSE file that accompanied this code.
    1.12 + *
    1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.15 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.16 + * version 2 for more details (a copy is included in the LICENSE file that
    1.17 + * accompanied this code).
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License version
    1.20 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.22 + *
    1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.24 + * or visit www.oracle.com if you need additional information or have any
    1.25 + * questions.
    1.26 + */
    1.27 +
    1.28 +/*
    1.29 + * This file is available under and governed by the GNU General Public
    1.30 + * License version 2 only, as published by the Free Software Foundation.
    1.31 + * However, the following notice accompanied the original version of this
    1.32 + * file:
    1.33 + *
    1.34 + * Written by Doug Lea with assistance from members of JCP JSR-166
    1.35 + * Expert Group and released to the public domain, as explained at
    1.36 + * http://creativecommons.org/publicdomain/zero/1.0/
    1.37 + */
    1.38 +
    1.39 +package java.util.concurrent;
    1.40 +import java.util.concurrent.atomic.*;
    1.41 +import java.util.*;
    1.42 +
    1.43 +/**
    1.44 + * An {@link ExecutorService} that can schedule commands to run after a given
    1.45 + * delay, or to execute periodically.
    1.46 + *
    1.47 + * <p> The <tt>schedule</tt> methods create tasks with various delays
    1.48 + * and return a task object that can be used to cancel or check
    1.49 + * execution. The <tt>scheduleAtFixedRate</tt> and
    1.50 + * <tt>scheduleWithFixedDelay</tt> methods create and execute tasks
    1.51 + * that run periodically until cancelled.
    1.52 + *
    1.53 + * <p> Commands submitted using the {@link Executor#execute} and
    1.54 + * {@link ExecutorService} <tt>submit</tt> methods are scheduled with
    1.55 + * a requested delay of zero. Zero and negative delays (but not
    1.56 + * periods) are also allowed in <tt>schedule</tt> methods, and are
    1.57 + * treated as requests for immediate execution.
    1.58 + *
    1.59 + * <p>All <tt>schedule</tt> methods accept <em>relative</em> delays and
    1.60 + * periods as arguments, not absolute times or dates. It is a simple
    1.61 + * matter to transform an absolute time represented as a {@link
    1.62 + * java.util.Date} to the required form. For example, to schedule at
    1.63 + * a certain future <tt>date</tt>, you can use: <tt>schedule(task,
    1.64 + * date.getTime() - System.currentTimeMillis(),
    1.65 + * TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a
    1.66 + * relative delay need not coincide with the current <tt>Date</tt> at
    1.67 + * which the task is enabled due to network time synchronization
    1.68 + * protocols, clock drift, or other factors.
    1.69 + *
    1.70 + * The {@link Executors} class provides convenient factory methods for
    1.71 + * the ScheduledExecutorService implementations provided in this package.
    1.72 + *
    1.73 + * <h3>Usage Example</h3>
    1.74 + *
    1.75 + * Here is a class with a method that sets up a ScheduledExecutorService
    1.76 + * to beep every ten seconds for an hour:
    1.77 + *
    1.78 + *  <pre> {@code
    1.79 + * import static java.util.concurrent.TimeUnit.*;
    1.80 + * class BeeperControl {
    1.81 + *   private final ScheduledExecutorService scheduler =
    1.82 + *     Executors.newScheduledThreadPool(1);
    1.83 + *
    1.84 + *   public void beepForAnHour() {
    1.85 + *     final Runnable beeper = new Runnable() {
    1.86 + *       public void run() { System.out.println("beep"); }
    1.87 + *     };
    1.88 + *     final ScheduledFuture<?> beeperHandle =
    1.89 + *       scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
    1.90 + *     scheduler.schedule(new Runnable() {
    1.91 + *       public void run() { beeperHandle.cancel(true); }
    1.92 + *     }, 60 * 60, SECONDS);
    1.93 + *   }
    1.94 + * }}</pre>
    1.95 + *
    1.96 + * @since 1.5
    1.97 + * @author Doug Lea
    1.98 + */
    1.99 +public interface ScheduledExecutorService extends ExecutorService {
   1.100 +
   1.101 +    /**
   1.102 +     * Creates and executes a one-shot action that becomes enabled
   1.103 +     * after the given delay.
   1.104 +     *
   1.105 +     * @param command the task to execute
   1.106 +     * @param delay the time from now to delay execution
   1.107 +     * @param unit the time unit of the delay parameter
   1.108 +     * @return a ScheduledFuture representing pending completion of
   1.109 +     *         the task and whose <tt>get()</tt> method will return
   1.110 +     *         <tt>null</tt> upon completion
   1.111 +     * @throws RejectedExecutionException if the task cannot be
   1.112 +     *         scheduled for execution
   1.113 +     * @throws NullPointerException if command is null
   1.114 +     */
   1.115 +    public ScheduledFuture<?> schedule(Runnable command,
   1.116 +                                       long delay, TimeUnit unit);
   1.117 +
   1.118 +    /**
   1.119 +     * Creates and executes a ScheduledFuture that becomes enabled after the
   1.120 +     * given delay.
   1.121 +     *
   1.122 +     * @param callable the function to execute
   1.123 +     * @param delay the time from now to delay execution
   1.124 +     * @param unit the time unit of the delay parameter
   1.125 +     * @return a ScheduledFuture that can be used to extract result or cancel
   1.126 +     * @throws RejectedExecutionException if the task cannot be
   1.127 +     *         scheduled for execution
   1.128 +     * @throws NullPointerException if callable is null
   1.129 +     */
   1.130 +    public <V> ScheduledFuture<V> schedule(Callable<V> callable,
   1.131 +                                           long delay, TimeUnit unit);
   1.132 +
   1.133 +    /**
   1.134 +     * Creates and executes a periodic action that becomes enabled first
   1.135 +     * after the given initial delay, and subsequently with the given
   1.136 +     * period; that is executions will commence after
   1.137 +     * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
   1.138 +     * <tt>initialDelay + 2 * period</tt>, and so on.
   1.139 +     * If any execution of the task
   1.140 +     * encounters an exception, subsequent executions are suppressed.
   1.141 +     * Otherwise, the task will only terminate via cancellation or
   1.142 +     * termination of the executor.  If any execution of this task
   1.143 +     * takes longer than its period, then subsequent executions
   1.144 +     * may start late, but will not concurrently execute.
   1.145 +     *
   1.146 +     * @param command the task to execute
   1.147 +     * @param initialDelay the time to delay first execution
   1.148 +     * @param period the period between successive executions
   1.149 +     * @param unit the time unit of the initialDelay and period parameters
   1.150 +     * @return a ScheduledFuture representing pending completion of
   1.151 +     *         the task, and whose <tt>get()</tt> method will throw an
   1.152 +     *         exception upon cancellation
   1.153 +     * @throws RejectedExecutionException if the task cannot be
   1.154 +     *         scheduled for execution
   1.155 +     * @throws NullPointerException if command is null
   1.156 +     * @throws IllegalArgumentException if period less than or equal to zero
   1.157 +     */
   1.158 +    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
   1.159 +                                                  long initialDelay,
   1.160 +                                                  long period,
   1.161 +                                                  TimeUnit unit);
   1.162 +
   1.163 +    /**
   1.164 +     * Creates and executes a periodic action that becomes enabled first
   1.165 +     * after the given initial delay, and subsequently with the
   1.166 +     * given delay between the termination of one execution and the
   1.167 +     * commencement of the next.  If any execution of the task
   1.168 +     * encounters an exception, subsequent executions are suppressed.
   1.169 +     * Otherwise, the task will only terminate via cancellation or
   1.170 +     * termination of the executor.
   1.171 +     *
   1.172 +     * @param command the task to execute
   1.173 +     * @param initialDelay the time to delay first execution
   1.174 +     * @param delay the delay between the termination of one
   1.175 +     * execution and the commencement of the next
   1.176 +     * @param unit the time unit of the initialDelay and delay parameters
   1.177 +     * @return a ScheduledFuture representing pending completion of
   1.178 +     *         the task, and whose <tt>get()</tt> method will throw an
   1.179 +     *         exception upon cancellation
   1.180 +     * @throws RejectedExecutionException if the task cannot be
   1.181 +     *         scheduled for execution
   1.182 +     * @throws NullPointerException if command is null
   1.183 +     * @throws IllegalArgumentException if delay less than or equal to zero
   1.184 +     */
   1.185 +    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
   1.186 +                                                     long initialDelay,
   1.187 +                                                     long delay,
   1.188 +                                                     TimeUnit unit);
   1.189 +
   1.190 +}