/**
* executes the given command at some time in the future. the command
* may execute in a new thread, in a pooled thread, or in the calling
* thread, at the discretion of the implementation.
** @param command the runnable task 執行runnale任務,定義乙個類實現runnale,或者直接使用lambda實現()->{}
* @throws rejectedexecutionexception if this task cannot be
* accepted for execution
* @throws nullpointerexception if command is null
*/void execute(runnable command);
/** * submits a value-returning task for execution and returns a
* future representing the pending results of the task. the
* future's method will return the task's result upon
* successful completion.
** * if you would like to immediately block waiting
* for a task, you can use constructions of the form* *
* note: the class includes a set of methods
* that can convert some other common closure-like objects,
* for example, to
* form so they can be submitted.
** @param task the task to submit 定義乙個類實現
* @param the type of the task's result 可以指定返回值型別
* @return a future representing pending completion of the task
* @throws rejectedexecutionexception if the task cannot be
* scheduled for execution
* @throws nullpointerexception if the task is null
*/futuresubmit(callabletask);
/*** submits a runnable task for execution and returns a future
* representing that task. the future's method will
* return the given result upon successful completion.
** @param task the task to submit 執行runnale任務,定義乙個類實現runnale,或者直接使用lambda實現()->{}
* @param result the result to return 指定型別返回
* @param the type of the result
* @return a future representing pending completion of the task
* @throws rejectedexecutionexception if the task cannot be
* scheduled for execution
* @throws nullpointerexception if the task is null
*/futuresubmit(runnable task, t result);
/*** submits a runnable task for execution and returns a future
* representing that task. the future's method will
* return upon successful completion.
** @param task the task to submit
* @return a future representing pending completion of the task 返回乙個任意型別的返回值
* @throws rejectedexecutionexception if the task cannot be
* scheduled for execution
* @throws nullpointerexception if the task is null
*/future<?> submit(runnable task);
//建立 執行緒池
// 使用 runnable
public class t03_runnable );
futurelist.add(submit);
}try
system.out.println(future.get());
}} catch (interruptedexception | executionexception e)
//public void work()
}// 使用 callable
public class t03_callable
};executorservice executorservice = executors.newsinglethreadexecutor();
futurefuture = executorservice.submit(callable);
// // future.get() :獲取非同步執行的結果,如果沒有結果可用,此方法會阻塞直到非同步計算完成
string s = future.get();
system.err.println(s);
executorservice.shutdown();
}}
參考: 執行緒池的實現方式 ExecutorService
使用 threadpoolexecutor 實現執行緒池的建立,有4個不同的構造方法。public threadpoolexecutor int corepoolsize,int maximumpoolsize,long keepalivetime,timeunit unit,blockingque...
執行緒 執行緒池
執行緒池是一種多執行緒處理形式,處理過程中將任務新增到佇列,然後在建立執行緒後執行,主要實現 建立執行緒和管理執行緒,並且給執行緒分配任務。執行緒池中的執行緒是併發執行的。乙個比較簡單的執行緒池至少應包含執行緒池管理器 工作執行緒 任務列隊 任務介面等部分。其中執行緒池管理器的作用是建立 銷毀並管理...
執行緒 執行緒池
乙個簡單執行緒的建立和銷毀如下 與程序程序相比,執行緒是一種輕量級的工具,但是輕量並不代表沒有,它的建立和關閉依然需要花費時間,如果建立和銷毀的時間還大於執行緒本身完成的工作,那就會得不償失,甚至會造成out of memory。即使沒有,大量的執行緒 也會給gc帶來巨大的壓力。為了解決這樣的問題,...