Java 學習記錄101 — Runnable and Thread

張小雄
9 min readJan 11, 2022

--

用第二種方法創造 Thread 也就是 runnable interface

新增 MyRunnable.java

package thread;import static thread.ThreadColor.ANSI_RED;public class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println(ANSI_RED + " Hello from MyRunnable's implementation of run()");
}
}

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();
Thread myRunnableThread = new Thread(new MyRunnable());
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 from AnotherThread.

Hello again from the main thread.

Hello from MyRunnable’s implementation of run()

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();
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();
System.out.println(ANSI_CYAN + "Hello again from the main thread.");
}
}

輸出結果:

Hello from the main thread.

Hello from AnotherThread.

Hello from the anonymous class thread.

Hello from MyRunnable’s implementation of run()

Hello again from the main thread.

Hello from MyRunnable’s implementation of run()

一樣也可以用 anonymous 來實現

因為有用 super.run() 所以原來的 method 也被調用了,所以輸出結果重複同個句子了兩次

不想重複調用的話,去掉就行,那就只會顯示 anonymous 這個 method

修改 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());
}
}

修改 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();
System.out.println(ANSI_CYAN + "Hello again from the main thread.");
}
}

輸出結果:

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 MyRunnable’s implementation of run()

Hello from == Another Thread ==

修改

Thread anotherThread = new AnotherThread();
anotherThread.setName(" == Another Thread == ");
anotherThread.run();

輸出結果:

Hello from the main thread.

Hello from main

Hello from the anonymous class thread.

Hello from MyRunnable’s implementation of run()

Hello again from the main thread.

Hello from anonymous MyRunnable class.

Tim 說我們使用上常見的錯誤有

沒有用 start() 而是用 run()

從這邊可以看出差別就是,前者有新增一個 thread,而後者只是在原本的 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");
}
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 MyRunnable’s implementation of run()

Hello from anonymous MyRunnable class.

Hello from == Another Thread ==

Three seconds have passed and I’m awake.

在 Thread 裡面加入睡眠 3 秒

如果被其他 Thread 打斷就走到 catch 裡面

因為沒被打斷所以就像輸出顯示的一樣往下走了

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

--

--

張小雄
張小雄

Written by 張小雄

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

No responses yet