现有3个线程,t1,t2,t3, 他们同时启动,如何计算三个线程全部执行完毕所发费的时间
2 回答
已采纳
绿洲仙人球
TA贡献39条经验 获得超47个赞
// 使用join方法,让主线程等待三个子线程执行结束,题主可以把这里的线程换成自己的 public static void main(String[] args) { MyRunnable runnable = new MyRunnable(); Thread main = Thread.currentThread(); long startTime = System.currentTimeMillis(); Thread thread1 = new Thread(runnable, "窗口1"); Thread thread2 = new Thread(runnable, "窗口2"); Thread thread3 = new Thread(runnable, "窗口3"); thread1.start(); thread2.start(); thread3.start(); try { thread1.join(); thread2.join(); thread3.join(); } catch (InterruptedException e) { e.printStackTrace(); } long endTime = System.currentTimeMillis(); long time = endTime - startTime; System.out.println("执行时间为:" + time); }
添加回答
举报
0/150
提交
取消