public interface ExecutorService extends Executorjava
ExecutorService繼承自Executor接口app
public interface Executor { /** * 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 <tt>Executor</tt> implementation. */ 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 <tt>get</tt> method will return the task's result upon * successful completion. */ <T> Future<T> submit(Callable<T> task); /** * Submits a Runnable task for execution and returns a Future * representing that task. The Future's <tt>get</tt> method will * return the given result upon successful completion. */ <T> Future<T> submit(Runnable task, T result); /** * Submits a Runnable task for execution and returns a Future * representing that task. The Future's <tt>get</tt> method will * return <tt>null</tt> upon <em>successful</em> completion. */ Future<?> submit(Runnable task);
/** * 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 <tt>Executor</tt> implementation. */ void execute(Runnable command);
import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class TestExecutorService { public static void main(String args[]) { List<String> data = new ArrayList<String>(); for (int i = 0; i < 1034; i++) { String str = String.valueOf(i); data.add(str); } int size = data.size(); int threadNum = 10; ExecutorService executorService = Executors.newFixedThreadPool(threadNum); List<Future<String>> futures = new ArrayList<Future<String>>(); for (int i = 0; i < threadNum; i++) { int fromIndex = 100 * i; int toIndex = 100 * (i + 1); if (i == threadNum - 1) { toIndex = size; } final List<String> subList = data.subList(fromIndex, toIndex); Callable<String> task = new Callable<String>() { @Override public String call() throws Exception { StringBuffer sb = new StringBuffer(); for (String str : subList) { sb.append(str + ","); } return sb.toString(); } }; Future<String> taskResult = executorService.submit(task); futures.add(taskResult); } long a = System.currentTimeMillis(); //shutdown() 方法在終止前容許執行之前提交的任務 //第一階段調用 shutdown 拒絕傳入任務,而後調用 shutdownNow(若有必要)取消全部遺留的任務 //提交的任務運行結束後關閉線程池 executorService.shutdown(); while (true) { /** * 經過不斷運行ExecutorService.isTerminated()方法檢測所有的線程是否都已經運行結束 */ if (executorService.isTerminated()) { System.out.println("全部任務執行完畢"); System.out.println("時間差=" + String.valueOf(System.currentTimeMillis() - a)); break; } try { //milliseconds Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } StringBuffer result = new StringBuffer(); for (Future<String> future : futures) { try { /** * V get() throws InterruptedException, ExecutionException; * 會拋出異常,能夠捕獲異常,當發生異常時,能夠選擇當即shutdown其餘任務 */ System.out.println("本次任務的執行結果:" + future.get()); result.append(future.get()); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { //當即shutdown其餘任務 executorService.shutdownNow(); e.printStackTrace(); } } System.out.println("最終結果:" + result.toString()); } }
ExecutorService之execute方法沒有返回結果,也不會經過處理結果返回異常。ide
這裏有execute方法的使用示例,經過實現Runnable接口定義了一個Task類,實現run方法spa
http://my.oschina.net/xinxingegeya/blog/263276.net
=============END=============線程