java线程池的应用
package com.test; import java.util.concurrent.Callable; public class TestThread implements Callable<Object>{ @Override public Object call() throws Exception { System.out.println(123); return 456; } }
package com.test; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class TestPool { public void handleThreadPool(){ int threadCount = 5; int poolSize = 3; TestThread[] tt = new TestThread[threadCount]; ExecutorService service = Executors.newFixedThreadPool(poolSize); Collection<TestThread> c = new ArrayList<TestThread>(); for (int i = 0; i < threadCount; i++) { tt[i] = new TestThread(); c.add(tt[i]); } try { List<Future<Object>> results = service.invokeAll(c); service.shutdown(); for (Future<Object> future : results) { System.out.println(future.get().toString()); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } } public static void main(String[] args){ TestPool tp = new TestPool(); tp.handleThreadPool(); } }
点亮测试人生!QQ:408129370
百度阅读电子书地址:http://yuedu.baidu.com/ebook/f6dbb2a2f01dc281e53af0f3
讨论QQ群:536192476
个人公众号:
百度阅读电子书地址:http://yuedu.baidu.com/ebook/f6dbb2a2f01dc281e53af0f3
讨论QQ群:536192476
个人公众号:
![](http://files.cnblogs.com/files/zhangfei/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91%E6%B1%9F%E6%B9%96.bmp)