1)public interface Executor{
void execute(Runnable command);
}
2) public interface ExecutorService
extends Executor { Future<?> submit(Runnable task); //get()方法返回null Future<T> submit(Runnable task, T result);//返回result Future<T> submit(Callable<T> task);//返回应该返回的 void shutdown(); //启动一次顺序关闭,执行以前提交的任务 //但不接受新任务。如果已经关闭,则调用没有其他作用。 List<Runnable> shutdownNow(); //试图停止所有正在执行的活动任务,暂停处理正在等待的任务,并返回等待执行的任务列表。//无法保证能够停止正在处理的活动执行任务,但是会尽力尝试。例如,通过 Thread.interrupt() 来取消典型的实
//现,所以任何任务无法响应中断都可能永远无法终止。
<T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
/*
*执行给定的任务,如果某个任务已成功完成(也就是未抛出异常),则返回其结果。
*一旦正常或异常返回后,则取消尚未完成的任务。
*如果此操作正在进行时修改了给定的 collection,则此方法的结果是不确定的。
*/
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException /* *执行给定的任务,当所有任务完成时,返回保持任务状态和结果的 Future 列表。返回列表的所有元素的 Future.isDone() 为* true。注意,可以正常地或通过抛出异常来终止 已完成 任务。如果正在进行此操作时修改了给定的 collection,则此方法的结果是*不确定的。 */
} When you are done with a thread pool, call shutdown. This method initiates the shutdown sequence for the pool. An executor that is shut down accepts no new tasks. When all tasks are finished, the threads in the pool die. Alternatively, you can call shutdownNow . The pool then cancels all tasks that have not yet begun and attempts to interrupt the running threads. 3) public interface ExecutorService extends Executor 4)public interface ScheduledExecutorService extends ExecutorService{} 5) newCachedThreadPool New threads are created as needed; idle threads are kept for 60 seconds. newFixedThreadPool The pool contains a fixed set of threads; idle threads are kept indefinitely. newSingleThreadExecutor A “pool” with a single thread that executes the submitted tasks sequentially (similar to the Swing event dispatch thread). newScheduledThreadPool A fixed-thread pool for scheduled execution; a replace- ment for java.util.Timer. newSingleThreadScheduledExecutor A single-thread “pool” for scheduled execution.
6)java.util.concurrent.ScheduledExecutorService 5.0
ScheduledFuture<V> schedule(Callable<V> task, long time, TimeUnit unit)
ScheduledFuture<?> schedule(Runnable task, long time, TimeUnit unit)
schedules the given task after the given time has elapsed.
ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long initialDelay, long period,
TimeUnit unit)
schedules the given task to run periodially, every period units, after the initial
delay has elapsed.
• ScheduledFuture<?> scheduleWithFixedDelay(Runnable task, long initialDelay, long delay,
TimeUnit unit)
schedules the given task to run periodially, with delay units between completion of
one invocation and the start of the next, after the initial delay has elapsed.
7) ExecutorCompletionService
List<Callable<T>> tasks = . . .;
List<Future<T>> results = executor.invokeAll(tasks);
for (Future<T> result : results)
processFurther(result.get());
ExecutorCompletionService service = new ExecutorCompletionService(executor);
for (Callable<T> task : tasks) service.submit(task);
for (int i = 0; i < tasks.size(); i++)
processFurther(service.take().get());