作者westinsane (西狂)
看板java
标题[问题] 执行绪方面的问题
时间Thu Jan 16 22:57:33 2014
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
1F:推 PsMonkey:==" 就 API 规格就是这样开... 在搞 thread 之前先 01/16 23:08
2F:→ PsMonkey:弄好基础吧.... Orz 01/16 23:08
3F:→ westinsane:歹势,小弟是新手^^",请问API规格是指? 01/16 23:12
4F:→ westinsane:我记得介面可以多重继承啊 01/16 23:16
5F:推 chunhsiang:你是不是自己写两个档案 一个是Runnable 一个是Shower? 01/17 08:20
6F:推 popcorny:你不需要自己写Runnable interface.. 01/17 09:22
7F:→ danny8376:不只是不需要 是不能自己写啊www 01/17 13:38
8F:→ westinsane:回c大:是,Runnable是介面,Shower是主程式 01/17 15:11
9F:→ chunhsiang:楼上已经给你答案 因为Thread的Constructor是接受 01/17 20:04
10F:→ chunhsiang:java.lang.Runnable而非你自订的Runnable 01/17 20:05
11F:→ westinsane:我了解了,把Runnable移除掉就没问题了,谢谢大家 01/17 20:35