public class Test { static volatile int i=0; static class T1 extends Thread { @Override public void run() { for(;i<100;i++); } } public static void main(String[] args) { T1 t1 = new T1(); t1.start(); try { // 主線程等待t1執行完畢,才繼續執行 t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(i); } }