看板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