package com.wangwenjun.concurrency.chapter5; public class ThreadJoin3 { public static void main(String[] args) throws InterruptedException { long startTimestamp = System.currentTimeMillis(); Thread t1 = new Thread(new CaptureRunnable("M1", 10000L)); Thread t2 = new Thread(new CaptureRunnable("M2", 30000L)); Thread t3 = new Thread(new CaptureRunnable("M3", 15000L)); t1.start(); t2.start(); t3.start(); t1.join(); t2.join(); t3.join(); //join能夠將上面三個線程並行執行,但不執行下面的線程,等待上面線程執行後再執行下面的輸出 long endTimestamp = System.currentTimeMillis(); System.out.printf("Save data begin timestamp is:%s, end timestamp is:%s\n", startTimestamp, endTimestamp); } } class CaptureRunnable implements Runnable { private String machineName; private long spendTime; public CaptureRunnable(String machineName, long spendTime) { this.machineName = machineName; this.spendTime = spendTime; } @Override public void run() { //do the really capture data. try { Thread.sleep(spendTime); System.out.printf(machineName + " completed data capture at timestamp [%s] and successfully.\n", System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } public String getResult() { return machineName + " finish."; } }