rt/emul/compact/src/main/java/java/util/concurrent/ScheduledExecutorService.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 10:46:31 +0100
branchjdk7-b147
changeset 1890 212417b74b72
permissions -rw-r--r--
Bringing in all concurrent package from JDK7-b147
jaroslav@1890
     1
/*
jaroslav@1890
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1890
     3
 *
jaroslav@1890
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1890
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1890
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1890
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1890
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1890
     9
 *
jaroslav@1890
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1890
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1890
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1890
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1890
    14
 * accompanied this code).
jaroslav@1890
    15
 *
jaroslav@1890
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@1890
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1890
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1890
    19
 *
jaroslav@1890
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1890
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1890
    22
 * questions.
jaroslav@1890
    23
 */
jaroslav@1890
    24
jaroslav@1890
    25
/*
jaroslav@1890
    26
 * This file is available under and governed by the GNU General Public
jaroslav@1890
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@1890
    28
 * However, the following notice accompanied the original version of this
jaroslav@1890
    29
 * file:
jaroslav@1890
    30
 *
jaroslav@1890
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@1890
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@1890
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1890
    34
 */
jaroslav@1890
    35
jaroslav@1890
    36
package java.util.concurrent;
jaroslav@1890
    37
import java.util.concurrent.atomic.*;
jaroslav@1890
    38
import java.util.*;
jaroslav@1890
    39
jaroslav@1890
    40
/**
jaroslav@1890
    41
 * An {@link ExecutorService} that can schedule commands to run after a given
jaroslav@1890
    42
 * delay, or to execute periodically.
jaroslav@1890
    43
 *
jaroslav@1890
    44
 * <p> The <tt>schedule</tt> methods create tasks with various delays
jaroslav@1890
    45
 * and return a task object that can be used to cancel or check
jaroslav@1890
    46
 * execution. The <tt>scheduleAtFixedRate</tt> and
jaroslav@1890
    47
 * <tt>scheduleWithFixedDelay</tt> methods create and execute tasks
jaroslav@1890
    48
 * that run periodically until cancelled.
jaroslav@1890
    49
 *
jaroslav@1890
    50
 * <p> Commands submitted using the {@link Executor#execute} and
jaroslav@1890
    51
 * {@link ExecutorService} <tt>submit</tt> methods are scheduled with
jaroslav@1890
    52
 * a requested delay of zero. Zero and negative delays (but not
jaroslav@1890
    53
 * periods) are also allowed in <tt>schedule</tt> methods, and are
jaroslav@1890
    54
 * treated as requests for immediate execution.
jaroslav@1890
    55
 *
jaroslav@1890
    56
 * <p>All <tt>schedule</tt> methods accept <em>relative</em> delays and
jaroslav@1890
    57
 * periods as arguments, not absolute times or dates. It is a simple
jaroslav@1890
    58
 * matter to transform an absolute time represented as a {@link
jaroslav@1890
    59
 * java.util.Date} to the required form. For example, to schedule at
jaroslav@1890
    60
 * a certain future <tt>date</tt>, you can use: <tt>schedule(task,
jaroslav@1890
    61
 * date.getTime() - System.currentTimeMillis(),
jaroslav@1890
    62
 * TimeUnit.MILLISECONDS)</tt>. Beware however that expiration of a
jaroslav@1890
    63
 * relative delay need not coincide with the current <tt>Date</tt> at
jaroslav@1890
    64
 * which the task is enabled due to network time synchronization
jaroslav@1890
    65
 * protocols, clock drift, or other factors.
jaroslav@1890
    66
 *
jaroslav@1890
    67
 * The {@link Executors} class provides convenient factory methods for
jaroslav@1890
    68
 * the ScheduledExecutorService implementations provided in this package.
jaroslav@1890
    69
 *
jaroslav@1890
    70
 * <h3>Usage Example</h3>
jaroslav@1890
    71
 *
jaroslav@1890
    72
 * Here is a class with a method that sets up a ScheduledExecutorService
jaroslav@1890
    73
 * to beep every ten seconds for an hour:
jaroslav@1890
    74
 *
jaroslav@1890
    75
 *  <pre> {@code
jaroslav@1890
    76
 * import static java.util.concurrent.TimeUnit.*;
jaroslav@1890
    77
 * class BeeperControl {
jaroslav@1890
    78
 *   private final ScheduledExecutorService scheduler =
jaroslav@1890
    79
 *     Executors.newScheduledThreadPool(1);
jaroslav@1890
    80
 *
jaroslav@1890
    81
 *   public void beepForAnHour() {
jaroslav@1890
    82
 *     final Runnable beeper = new Runnable() {
jaroslav@1890
    83
 *       public void run() { System.out.println("beep"); }
jaroslav@1890
    84
 *     };
jaroslav@1890
    85
 *     final ScheduledFuture<?> beeperHandle =
jaroslav@1890
    86
 *       scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS);
jaroslav@1890
    87
 *     scheduler.schedule(new Runnable() {
jaroslav@1890
    88
 *       public void run() { beeperHandle.cancel(true); }
jaroslav@1890
    89
 *     }, 60 * 60, SECONDS);
jaroslav@1890
    90
 *   }
jaroslav@1890
    91
 * }}</pre>
jaroslav@1890
    92
 *
jaroslav@1890
    93
 * @since 1.5
jaroslav@1890
    94
 * @author Doug Lea
jaroslav@1890
    95
 */
jaroslav@1890
    96
public interface ScheduledExecutorService extends ExecutorService {
jaroslav@1890
    97
jaroslav@1890
    98
    /**
jaroslav@1890
    99
     * Creates and executes a one-shot action that becomes enabled
jaroslav@1890
   100
     * after the given delay.
jaroslav@1890
   101
     *
jaroslav@1890
   102
     * @param command the task to execute
jaroslav@1890
   103
     * @param delay the time from now to delay execution
jaroslav@1890
   104
     * @param unit the time unit of the delay parameter
jaroslav@1890
   105
     * @return a ScheduledFuture representing pending completion of
jaroslav@1890
   106
     *         the task and whose <tt>get()</tt> method will return
jaroslav@1890
   107
     *         <tt>null</tt> upon completion
jaroslav@1890
   108
     * @throws RejectedExecutionException if the task cannot be
jaroslav@1890
   109
     *         scheduled for execution
jaroslav@1890
   110
     * @throws NullPointerException if command is null
jaroslav@1890
   111
     */
jaroslav@1890
   112
    public ScheduledFuture<?> schedule(Runnable command,
jaroslav@1890
   113
                                       long delay, TimeUnit unit);
jaroslav@1890
   114
jaroslav@1890
   115
    /**
jaroslav@1890
   116
     * Creates and executes a ScheduledFuture that becomes enabled after the
jaroslav@1890
   117
     * given delay.
jaroslav@1890
   118
     *
jaroslav@1890
   119
     * @param callable the function to execute
jaroslav@1890
   120
     * @param delay the time from now to delay execution
jaroslav@1890
   121
     * @param unit the time unit of the delay parameter
jaroslav@1890
   122
     * @return a ScheduledFuture that can be used to extract result or cancel
jaroslav@1890
   123
     * @throws RejectedExecutionException if the task cannot be
jaroslav@1890
   124
     *         scheduled for execution
jaroslav@1890
   125
     * @throws NullPointerException if callable is null
jaroslav@1890
   126
     */
jaroslav@1890
   127
    public <V> ScheduledFuture<V> schedule(Callable<V> callable,
jaroslav@1890
   128
                                           long delay, TimeUnit unit);
jaroslav@1890
   129
jaroslav@1890
   130
    /**
jaroslav@1890
   131
     * Creates and executes a periodic action that becomes enabled first
jaroslav@1890
   132
     * after the given initial delay, and subsequently with the given
jaroslav@1890
   133
     * period; that is executions will commence after
jaroslav@1890
   134
     * <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
jaroslav@1890
   135
     * <tt>initialDelay + 2 * period</tt>, and so on.
jaroslav@1890
   136
     * If any execution of the task
jaroslav@1890
   137
     * encounters an exception, subsequent executions are suppressed.
jaroslav@1890
   138
     * Otherwise, the task will only terminate via cancellation or
jaroslav@1890
   139
     * termination of the executor.  If any execution of this task
jaroslav@1890
   140
     * takes longer than its period, then subsequent executions
jaroslav@1890
   141
     * may start late, but will not concurrently execute.
jaroslav@1890
   142
     *
jaroslav@1890
   143
     * @param command the task to execute
jaroslav@1890
   144
     * @param initialDelay the time to delay first execution
jaroslav@1890
   145
     * @param period the period between successive executions
jaroslav@1890
   146
     * @param unit the time unit of the initialDelay and period parameters
jaroslav@1890
   147
     * @return a ScheduledFuture representing pending completion of
jaroslav@1890
   148
     *         the task, and whose <tt>get()</tt> method will throw an
jaroslav@1890
   149
     *         exception upon cancellation
jaroslav@1890
   150
     * @throws RejectedExecutionException if the task cannot be
jaroslav@1890
   151
     *         scheduled for execution
jaroslav@1890
   152
     * @throws NullPointerException if command is null
jaroslav@1890
   153
     * @throws IllegalArgumentException if period less than or equal to zero
jaroslav@1890
   154
     */
jaroslav@1890
   155
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
jaroslav@1890
   156
                                                  long initialDelay,
jaroslav@1890
   157
                                                  long period,
jaroslav@1890
   158
                                                  TimeUnit unit);
jaroslav@1890
   159
jaroslav@1890
   160
    /**
jaroslav@1890
   161
     * Creates and executes a periodic action that becomes enabled first
jaroslav@1890
   162
     * after the given initial delay, and subsequently with the
jaroslav@1890
   163
     * given delay between the termination of one execution and the
jaroslav@1890
   164
     * commencement of the next.  If any execution of the task
jaroslav@1890
   165
     * encounters an exception, subsequent executions are suppressed.
jaroslav@1890
   166
     * Otherwise, the task will only terminate via cancellation or
jaroslav@1890
   167
     * termination of the executor.
jaroslav@1890
   168
     *
jaroslav@1890
   169
     * @param command the task to execute
jaroslav@1890
   170
     * @param initialDelay the time to delay first execution
jaroslav@1890
   171
     * @param delay the delay between the termination of one
jaroslav@1890
   172
     * execution and the commencement of the next
jaroslav@1890
   173
     * @param unit the time unit of the initialDelay and delay parameters
jaroslav@1890
   174
     * @return a ScheduledFuture representing pending completion of
jaroslav@1890
   175
     *         the task, and whose <tt>get()</tt> method will throw an
jaroslav@1890
   176
     *         exception upon cancellation
jaroslav@1890
   177
     * @throws RejectedExecutionException if the task cannot be
jaroslav@1890
   178
     *         scheduled for execution
jaroslav@1890
   179
     * @throws NullPointerException if command is null
jaroslav@1890
   180
     * @throws IllegalArgumentException if delay less than or equal to zero
jaroslav@1890
   181
     */
jaroslav@1890
   182
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
jaroslav@1890
   183
                                                     long initialDelay,
jaroslav@1890
   184
                                                     long delay,
jaroslav@1890
   185
                                                     TimeUnit unit);
jaroslav@1890
   186
jaroslav@1890
   187
}