ExecutorService.submit(Callable).get()不并发执行
2010-12-24 10:58
337 查看
今天在学习Callable的用法,public class CallableTry {class Task implements Callable<Long> {private long times;private String name;public Task(long times, String name) {this.name = name;this.times = times;}@Overridepublic Long call() {System.out.println(name + "开始执行, time[" + times + "]...");long before = System.currentTimeMillis();for (int i = 0; i < times; i++);long after = System.currentTimeMillis();System.out.println(name + "执行结束.");long cost = after - before;System.out.println(name + "耗时 :" + cost);return cost;}}/*** @param args*/public static void main(String[] args) throws ExecutionException,InterruptedException {long total = 0;CallableTry tr = new CallableTry();ExecutorService pool = Executors.newCachedThreadPool();Random rand = new Random();int count = 10;for (int i = 0; i < count; i++) {total += pool.submit(tr.new Task(10000000 * rand.nextInt(100), i + "任务")).get();System.out.println("next task...");}pool.shutdown();while (!pool.isTerminated());System.out.println("耗时:" + total + "毫秒, 平均用时:" + total * 1.0 / count+ "毫秒");}}打印结果为:0任务开始执行, time[860000000]...0任务执行结束.0任务耗时 :4750next task...1任务开始执行, time[430000000]...1任务执行结束.1任务耗时 :2343next task...2任务开始执行, time[500000000]...2任务执行结束.2任务耗时 :1703next task...3任务开始执行, time[990000000]...3任务执行结束.3任务耗时 :3344next task...4任务开始执行, time[340000000]...4任务执行结束.4任务耗时 :1156next task...5任务开始执行, time[640000000]...5任务执行结束.5任务耗时 :2250next task...6任务开始执行, time[880000000]...6任务执行结束.6任务耗时 :3032next task...7任务开始执行, time[800000000]...7任务执行结束.7任务耗时 :2781next task...8任务开始执行, time[450000000]...8任务执行结束.8任务耗时 :1547next task...9任务开始执行, time[980000000]...9任务执行结束.9任务耗时 :3375next task...耗时:26281毫秒, 平均用时:2628.1毫秒运行了很多次,时间上有差异,但是执行顺序却一直是这个顺序...而把
total += pool.submit( tr.new Task(10000000 * rand.nextInt(100), i + "任务")).get();的.get()去掉后 结果是:next task...next task...0任务开始执行, time[880000000]...next task...next task...next task...next task...next task...next task...next task...next task...2任务开始执行, time[790000000]...1任务开始执行, time[230000000]...3任务开始执行, time[270000000]...4任务开始执行, time[490000000]...5任务开始执行, time[950000000]...6任务开始执行, time[950000000]...7任务开始执行, time[630000000]...8任务开始执行, time[510000000]...9任务开始执行, time[740000000]...1任务执行结束.1任务耗时 :44843任务执行结束.3任务耗时 :50947任务执行结束.7任务耗时 :82814任务执行结束.4任务耗时 :90799任务执行结束.9任务耗时 :90008任务执行结束.8任务耗时 :98605任务执行结束.5任务耗时 :148436任务执行结束.6任务耗时 :156412任务执行结束.2任务耗时 :182970任务执行结束.0任务耗时 :19000
是不是.get()之后就不并发执行了呢?那它还是concurrency吗?
相关文章推荐
- Java并发专题 带返回结果的批量任务执行 CompletionService ExecutorService.invokeAll
- Java并发编程之Future,ExecutorService,Callable讲解实例
- ExecutorService——<T> Future<T> submit(Callable<T> task)
- 线程池ExecutorService 中并发数的(引入信号量Semaphore)控制执行
- JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都没
- Java并发专题 带返回结果的批量任务执行 CompletionService ExecutorService.invokeAll
- Java并发专题 带返回结果的批量任务执行 CompletionService ExecutorService.invokeAll
- Java并发专题 带返回结果的批量任务执行 CompletionService ExecutorService.invokeAll
- 多线程和并发中新的任务执行架构Executor、ExecutorService、ScheduledExecutorService
- Java Executor并发框架(九)Executor框架线程池ExecutorService.shutdown什么时候执行
- Java并发编程 - Executor,Executors,ExecutorService, CompletionServie,Future,Callable
- Java并发专题 带返回结果的批量任务执行 CompletionService ExecutorService.invokeAll
- JAVA线程池之ExecutorService Future Callable Submit简单应用
- Java并发编程:ScheduledExecutorService执行周期任务
- Java并发专题 带返回结果的批量任务执行 CompletionService ExecutorService.invokeAll
- Java并发专题 带返回结果的批量任务执行 CompletionService ExecutorService.invokeAll
- java利用线程池(ExecutorService)配合Callable和Future实现执行方法超时的阻断
- Java并发专题 带返回结果的批量任务执行 CompletionService ExecutorService.invokeAll(转)
- ExecutorService中submit和execute的区别、Callable 的用法
- ScheduledExecutorService 预定执行任务