java併發編程學習之線程池-Executor和ExecutorService(一)

Executor接口

void execute(Runnable command)//用於提交command任務

ExecutorService接口

繼承了Executor接口線程

//設置線程的狀態,還沒執行的線程會被中斷
void shutdown();
//設置線程的狀態,嘗試中止正在進行的線程
List<Runnable> shutdownNow();
//當調用shutdown()或shutdownNow()方法後返回爲true
boolean isShutdown();
//當調用shutdown()方法後,而且全部提交的任務完成後返回爲true
//當調用shutdownNow()方法後,成功中止後返回爲true;
boolean isTerminated();
//當前線程阻塞,直到線程執行完、時間到、被中斷
boolean awaitTermination(long timeout, TimeUnit unit)
    throws InterruptedException;
//提交一個Callable任務
<T> Future<T> submit(Callable<T> task);
//提交一個Runnable任務,由於Runnable沒有返回指,因此第二個參數是用來返回值
<T> Future<T> submit(Runnable task, T result);
//提交一個Runnable任務
Future<?> submit(Runnable task);
//執行全部任務
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
    throws InterruptedException;
//執行全部任務,有過時時間
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
                              long timeout, TimeUnit unit)
    throws InterruptedException;
//有一個任務結束就能夠返回
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
                long timeout, TimeUnit unit)
    throws InterruptedException, ExecutionException, TimeoutException;
//有一個任務結束就能夠返回,有過時時間
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
                    long timeout, TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException;
相關文章
相關標籤/搜索