修改 Main.java
package thread;import static thread.ThreadColor.*;public class Main { public static void main(String[] args) {
System.out.println(ANSI_PURPLE + "Hello from the main thread."); Thread anotherThread = new AnotherThread();
anotherThread.setName(" == Another Thread == ");
anotherThread.start(); new Thread() {
@Override
public void run() {
System.out.println(ANSI_GREEN + "Hello from the anonymous class thread.");
}
}.start(); Thread myRunnableThread = new Thread(new MyRunnable());
myRunnableThread.start(); Thread myRunnableThread2 = new Thread(new MyRunnable(){
@Override
public void run() {
System.out.println(ANSI_BLACK + "Hello from anonymous MyRunnable class.");
// super.run();
}
});
myRunnableThread2.start();
anotherThread.interrupt(); System.out.println(ANSI_CYAN + "Hello again from the main thread.");
}
}
修改 AnotherThread.java
package thread;import static thread.ThreadColor.ANSI_BLUE;public class AnotherThread extends Thread {
@Override
public void run() {
System.out.println(ANSI_BLUE + "Hello from " + currentThread().getName()); try {
// 3 sec
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println(ANSI_BLUE + " Another thread woke me up");
return;
} System.out.println(ANSI_BLUE + "Three seconds have passed and I'm awake.");
}
}
輸出結果:
Hello from the main thread.
Hello again from the main thread.
Hello from the anonymous class thread.
Hello from anonymous MyRunnable class.
Hello from MyRunnable’s implementation of run()
Hello from == Another Thread ==
Another thread woke me up
嘗試打斷 anotherThread
進入到 catch
裡面
可以從輸出結果最後一句發現的確有進到 catch
修改 Main.java
package thread;import static thread.ThreadColor.*;public class Main { public static void main(String[] args) {
System.out.println(ANSI_PURPLE + "Hello from the main thread."); final Thread anotherThread = new AnotherThread();
anotherThread.setName(" == Another Thread == ");
anotherThread.start(); new Thread() {
@Override
public void run() {
System.out.println(ANSI_GREEN + "Hello from the anonymous class thread.");
}
}.start(); Thread myRunnableThread = new Thread(new MyRunnable() {
@Override
public void run() {
System.out.println(ANSI_BLACK + "Hello from anonymous MyRunnable class.");
try {
anotherThread.join();
System.out.println(ANSI_RED + "AnotherThread terminated, so I'm running again." );
} catch (InterruptedException e) {
System.out.println(ANSI_RED + "I couldn't wait after all. I was interrupted");
}
}
});
myRunnableThread.start(); System.out.println(ANSI_CYAN + "Hello again from the main thread.");
}
}
輸出結果:
Hello from the main thread.
Hello from the anonymous class thread.
Hello again from the main thread.
Hello from anonymous MyRunnable class.
Hello from == Another Thread ==
Three seconds have passed and I’m awake.
AnotherThread terminated, so I’m running again.
在 myRunnableThread
執行過程中加入 anotherThread
從輸出結果的倒數那四句,可以看出執行的過程跟順序
簡單的說 join()
就是去執行另一個 thread,並且會等到其執行完
修改 Main.java
anotherThread.join(1000);
輸出結果:
Hello from the main thread.
Hello from the anonymous class thread.
Hello again from the main thread.
Hello from anonymous MyRunnable class.
Hello from == Another Thread ==
AnotherThread terminated or timed out, so I’m running again.
Three seconds have passed and I’m awake.
Tim 說:假設另一個 thread 一直運行都沒有結束,那麼就會影響到原來的 thread
所以可以加入時間限制,最多就等 x 秒,結束後程序就會往下走,不會一直卡在那邊等結束
這邊加入了 1000 毫秒(1 秒)的限制
值得注意的是從輸出結果來看,anotherThread
並沒有被終結掉,最終還是有順利執行完
加入這個限制,可以預防某個 thread 運行到一半出現預料外的 bug 卡住,連帶影響到整個程序
Tim 還說了,有關 thread 的方面,不同系統執行的結果可能不盡相同,所以不能夠要求程序百分之百照你所想執行