java中給出一個主線程要join子線程的例子

下面給出了一個join和interrupt互動的例子,還是主線程要join子線程。
例:1.5.3_1-本章源碼
class ThreadMark_to_win extends Thread {
    Thread mainT;
    int e;
    public void run() {
        for (int i = 0; i < 10; i++)
        {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
            if(i==4) mainT.interrupt();
            e = e + i;
        }
        System.out.println("完成"+"e 在子線程"+e);
    }
    public void setMainThread(Thread t1) {
        mainT=t1;
      
    }
}
public class Test {
    public static void main(String[] args) {
        Thread mainT = Thread.currentThread();
        ThreadMark_to_win tm = new ThreadMark_to_win();
        tm.setMainThread(mainT);
        tm.start();
        try {
            tm.join();
        } catch (InterruptedException e) {
            System.out.println("我是主程序, 也被打斷");
        }
        System.out.println("主線程e = " + tm.e);
    }
}
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。
輸出結果:
我是主程序, 也被打斷
主線程e = 10
完成e 在子線程45

后續(xù):
馬克-to-win:在上述的例子中:主線程還是想join子線程,但子線程當計算到i==4時,打斷了主線程,所以主線程打出e=10;但子線程繼續(xù)自己的線程運行,所以打印出“完成e 在子線程45”