Executors

 

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 ExecutorServiceextends Executor
4)public interface ScheduledExecutorServiceextends 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());  

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值