看板java
標 題[問題]關於thread.....
發信站SayYA 資訊站 (Mon Mar 20 11:37:39 2006)
轉信站ptt!ctu-reader!news.nctu!SayYa
請問各位大大一個問題:
這是一個thread的程式。,
為什麼他一開始不用
去有import java.io.*;\
nfl 它不是也用了Syste,m.out.println 這個method嗎?
class Worker extends Thread {
//constructor
public Worker(String name) {
super(name);
}
//開始做事
public void run() {
int cars = 6;
try {
for (int i = 0; i < cars; i++) {
System.out.println(i + " " + getName() +
", fixing car-" + i);
sleep((long)(Math.random() * 500));
}
System.out.println("Yes, all finished! I'm " +
getName());
} catch (InterruptedException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
}
//啟動主程式
public class PushWork1 {
public static void main (String[] args) {
int emps = 5;
for (int i = 0; i < emps; ++i)
new Worker("CarWorker #"+i).start(); //生成threads
}
}
--
※ Origin: SayYA 資訊站 <bbs.sayya.org>
◆ From: 140.109.176.101