[問題] 執行緒方面的問題
public interface Runnable {
public abstract void run();
}
class FatherThread implements Runnable {
public void run() {
System.out.println("爸爸下班回家.");
System.out.println("爸爸準備洗澡.");
System.out.println("爸爸發現沒瓦斯了.");
System.out.println("爸爸打電話請瓦斯工人送瓦斯.");
Thread worker = new Thread(new WorkerThread());
System.out.println("爸爸等待瓦斯工人...");
worker.start();
try {
worker.join();
}
catch(InterruptedException e) {
System.out.println("爸爸決定今天不洗熱水澡了 !");
}
System.out.println("爸爸開始洗澡 !");
System.out.println("爸爸洗完澡了 !");
}
}
class WorkerThread implements Runnable {
public void run() {
System.out.println();
System.out.println("瓦斯工人送瓦斯中...");
try {
for(int i=1; i<=5; i++) {
Thread.sleep(1000);
System.out.print(i + " 分鐘, ");
}
}
catch(InterruptedException ie) {
System.err.println("瓦斯工人送瓦斯途中發生意外 !");
}
System.out.println();
System.out.println("瓦斯工人將瓦斯送到家了 !");
System.out.println("瓦斯工人將瓦斯安裝完畢 !");
System.out.println();
}
}
public class Shower {
public static void main(String[] args) {
Thread father = new Thread(new FatherThread());
father.start();
}
}
我剛才實作了這個範例,用 FatherThread 及 WorkerThread implements Runnable 的
介面,可是在 FatherThread 中的其中一行
Thread worker = new Thread(new WorkerThread());
及主程式 Shower 的其中一行
Thread father = new Thread(new FatherThread());
的等號右邊均出現了
The constructor Thread(WorkerThread/FatherThread) is undefined
這個問題,請問一下為什麼會這樣?且我若將 FatherThread 及 WorkerThread 的
implements Runnable 改成 extends Thread 就又可以解決了,這又是什麼原因呢
?懇請板上的眾高手幫忙解除我的疑惑,感激不盡
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 1.171.161.156
推
01/16 23:08, , 1F
01/16 23:08, 1F
→
01/16 23:08, , 2F
01/16 23:08, 2F
→
01/16 23:12, , 3F
01/16 23:12, 3F
→
01/16 23:16, , 4F
01/16 23:16, 4F
推
01/17 08:20, , 5F
01/17 08:20, 5F
推
01/17 09:22, , 6F
01/17 09:22, 6F
→
01/17 13:38, , 7F
01/17 13:38, 7F
→
01/17 15:11, , 8F
01/17 15:11, 8F
→
01/17 20:04, , 9F
01/17 20:04, 9F
→
01/17 20:05, , 10F
01/17 20:05, 10F
→
01/17 20:35, , 11F
01/17 20:35, 11F
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章