privatevoidloadMock(String source, int second){ try { long startTime = System.currentTimeMillis(); long milliseconds = TimeUnit.SECONDS.toMillis(second);
// 模拟任务执行 Thread.sleep(milliseconds);
long endTime = System.currentTimeMillis(); System.out.printf("[线程: %s] %s 耗时: %d ms \n", Thread.currentThread().getName(),source,endTime-startTime);
int count = 0; while (count < 3) { // 移除队列,模拟轮询获取返回 if (completionService.poll() != null) { count++; } } executorService.shutdown(); long end = System.currentTimeMillis(); System.out.printf("load() 总耗时: %d ms \n", end - start); }
publicstaticvoidmain(String[] args){ new ParallelDataLoader().load(); //[线程: pool-1-thread-1] loadConfiguration() 耗时: 1001 ms //[线程: pool-1-thread-2] loadUsers() 耗时: 2000 ms //[线程: pool-1-thread-3] loadOrders() 耗时: 3000 ms //load() 总耗时: 3089 ms } }