问程序结束运行时间。
?
import java.util.ArrayList;
import java.util.Date;
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 TestThread {public static void main(String[] args) throws ExecutionException, InterruptedException {System.out.println("----程序开始运行----");Date date1 = new Date();int taskSize = 5;// 创建一个线程池ExecutorService pool = Executors.newFixedThreadPool(taskSize);// 创建多个有返回值的任务List<Future> list = new ArrayList();for (int i = 0; i < taskSize; i++) {Callable c;if(i==2){c = new MyCallable(i + " ",10000L);}else {c = new MyCallable(i + " ",1000L);}Future f = pool.submit(c);list.add(f);}// 关闭线程池pool.shutdown();// 获取所有并发任务的运行结果for (Future f : list) {// 从Future对象上获取任务的返回值,并输出到控制台System.out.println(">>>" + f.get().toString());}Date date2 = new Date();System.out.println("----程序结束运行----,程序运行时间【" + (date2.getTime() - date1.getTime()) + "毫秒】");}
}class MyCallable implements Callable {private String taskNum;private Long time;MyCallable(String taskNum,Long tmie) {this.time = tmie;this.taskNum = taskNum;}public Object call() throws Exception {Date dateTmp1 = new Date();System.out.println(">>>" + dateTmp1.getTime() + "进入线程时间");System.out.println(">>>" + taskNum + "任务启动");Thread.sleep(time);Date dateTmp2 = new Date();long time = dateTmp2.getTime() - dateTmp1.getTime();System.out.println(">>>" + taskNum + "任务终止");return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";}
}?
两个worker的时候又如何
import java.util.ArrayList;
import java.util.Date;
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 TestThread {public static void main(String[] args) throws ExecutionException, InterruptedException {System.out.println("----程序开始运行----");Date date1 = new Date();int taskSize = 5;// 创建一个线程池ExecutorService pool = Executors.newFixedThreadPool(taskSize);// 创建多个有返回值的任务List<Future> list = new ArrayList();MyCallable myCallable = new MyCallable("1", 1000L);Future submit1 = pool.submit(myCallable);MyCallable2 myCallable2 = new MyCallable2("2", 10000L);Future submit2 = pool.submit(myCallable2);list.add(submit1);list.add(submit2);// 获取所有并发任务的运行结果for (Future f : list) {// 从Future对象上获取任务的返回值,并输出到控制台System.out.println(">>>" + f.get().toString());}Date date2 = new Date();System.out.println("----程序结束运行----,程序运行时间【" + (date2.getTime() - date1.getTime()) + "毫秒】");// 关闭线程池pool.shutdown();}
}class MyCallable implements Callable {private String taskNum;private Long time;MyCallable(String taskNum,Long tmie) {this.time = tmie;this.taskNum = taskNum;}public Object call() throws Exception {Date dateTmp1 = new Date();System.out.println(">>>" + dateTmp1.getTime() + "worker1进入线程时间");Thread.sleep(time);Date dateTmp2 = new Date();long time = dateTmp2.getTime() - dateTmp1.getTime();return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";}
}class MyCallable2 implements Callable {private String taskNum;private Long time;MyCallable2(String taskNum,Long tmie) {this.time = tmie;this.taskNum = taskNum;}public Object call() throws Exception {Date dateTmp1 = new Date();System.out.println(">>>" + dateTmp1.getTime() + "worker2进入线程时间");Thread.sleep(time);Date dateTmp2 = new Date();long time = dateTmp2.getTime() - dateTmp1.getTime();return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";}
}