()
| 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void add() throws ExecutionException, InterruptedException { |
| 57 | System.out.println("----程序开始运行----"); |
| 58 | Date date1 = new Date(); |
| 59 | |
| 60 | // 线程池数量 |
| 61 | int taskSize = 5; |
| 62 | // 创建一个线程池 |
| 63 | ExecutorService executorService = Executors.newFixedThreadPool(taskSize); |
| 64 | // 创建多个有返回值的任务 |
| 65 | List<Future<Object>> list = new ArrayList<>(); |
| 66 | for (int i = 0; i < taskSize; i++) { |
| 67 | Callable c = new MyCallable(i + " "); |
| 68 | // 执行任务并获取Future对象 |
| 69 | Future<Object> f = executorService.submit(c); |
| 70 | System.out.println(">>>" + f.get().toString()); |
| 71 | list.add(f); |
| 72 | } |
| 73 | // 关闭线程池 |
| 74 | executorService.shutdown(); |
| 75 | |
| 76 | // 获取所有并发任务的运行结果 |
| 77 | for (Future<Object> f : list) { |
| 78 | // 从Future对象上获取任务的返回值,并输出到控制台 |
| 79 | System.out.println(">>>" + f.get().toString()); |
| 80 | } |
| 81 | |
| 82 | Date date2 = new Date(); |
| 83 | System.out.println("----程序结束运行----,程序运行时间【" + (date2.getTime() - date1.getTime()) + "毫秒】"); |
| 84 | } |
| 85 | |
| 86 | // private void getdirs(String path){ |
| 87 | // String pocPath = PocUtil.PocPath+"json"+ File.separator; |
no test coverage detected