Java 學習記錄99 — Threads

張小雄
7 min readDec 23, 2021

--

今天我們要開始學習 Concurrency

從 Thread 開始學

package thread;public class Main {    public static void main(String[] args) {
System.out.println("Hello from the main thread.");
}
}

輸出結果:

Hello from the main thread.

接著創 AnotherThread.java

package thread;public class AnotherThread extends Thread{
@Override
public void run() {
System.out.println("Hello from AnotherThread.");
}
}

回到 Main

package thread;public class Main {    public static void main(String[] args) {
System.out.println("Hello from the main thread.");
Thread anotherThread = new AnotherThread();
anotherThread.start();
}
}

輸出結果:

Hello from the main thread.

Hello from AnotherThread.

新增了一個 AnotherThread 的實例並啟動

只有創實例但沒有 start() 的話是不會發揮功能的

繼續新增一行問候語

package thread;public class Main {    public static void main(String[] args) {
System.out.println("Hello from the main thread.");
Thread anotherThread = new AnotherThread();
anotherThread.start();
System.out.println("Hello again from the main thread."); }
}

輸出結果:

Hello from the main thread.

Hello again from the main thread.

Hello from AnotherThread.

Tim 老師說每個機子,甚至同機子每次執行的結果是不可預期的

我的機子重複跑了 10 次結果都一樣,懶得測了

System.out.println("Hello from the main thread.");Thread anotherThread = new AnotherThread();
anotherThread.start();
System.out.println("Hello again from the main thread.");anotherThread.start();

輸出結果:

Hello from the main thread.

Hello again from the main thread.

Hello from AnotherThread.

Exception in thread “main” java.lang.IllegalThreadStateException

at java.base/java.lang.Thread.start(Thread.java:789)

at thread.Main.main(Main.java:13)

同個 instance 的 start() 不能被重複使用,會報錯

System.out.println("Hello from the main thread.");Thread anotherThread = new AnotherThread();
anotherThread.start();
new Thread(){
@Override
public void run() {
System.out.println("Hello from the anonymous class thread.");
}
}.start();
System.out.println("Hello again from the main thread.");

輸出結果:

Hello from the main thread.

Hello again from the main thread.

Hello from AnotherThread.

Hello from the anonymous class thread.

這裡使用了 anonymous class 來新增一個 thread

好處是不用另外創個 class,適合一次性使用

壞處是無法重複使用

新增 ThreadColor.java

package thread;public class ThreadColor {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
}

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 AnotherThread.");
}
}

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.start();
new Thread(){
@Override
public void run() {
System.out.println(ANSI_GREEN + "Hello from the anonymous class thread.");
}
}.start();
System.out.println(ANSI_CYAN + "Hello again from the main thread."); }
}

輸出結果:

Hello from the main thread.

Hello from AnotherThread.

Hello again from the main thread.

Hello from the anonymous class thread.

把每一句話分別對應上不同顏色

這裡顯示不出來,但跑在 Interllij 上的話會有不同

上面代碼全都紀錄在我的 Github

--

--

張小雄
張小雄

Written by 張小雄

記錄成為軟體工程師的過程

No responses yet