public static void main(String[] args) {
ScheduledExecutorService se=Executors.newScheduledThreadPool(1);
for (int i = 0; i < 5; i++) {
se.execute(new MyThead(i));
}
}
}
class MyThead implements Runnable{
int count,number;
public MyThead(int unmber) {
super();
this.number = unmber;
System.out.println("thread-"+number+"is new");
}
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);
System.out.println("thread"+number+"count="+count);
count++;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
运行结果如下:
thread-0is new
thread-1is new
thread-2is new
thread-3is new
thread-4is new
thread0count=0
thread0count=1
thread0count=2
thread0count=3
thread0count=4
thread0count=5
thread0count=6.........
执行以后为什么输出五个线程都被new出来了。 可是为什么只有一个线程在执行?