public static void main(String[] args) {
// TODO Auto-generated method stub
ExecutorService pool= Executors.newCachedThreadPool(); /*得到一个线程池*/
for(int i=0;i<10;i++){
final int taskid=i;
pool.execute(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
System.out.println(Thread.currentThread().getName()+
"is execute task "+ taskid+"for"+i
+"times !");
}
}
});
}
pool.shutdown();/*结束线程*/
}
}
定时器:
/* 定时器:调度池,每隔多时间去做一下*/
ScheduledExecutorService time= Executors.newScheduledThreadPool(1);
time.scheduleAtFixedRate(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if(vechicies.size()>0){
boolean lighted=Lamp.valueOf(Road.this.name).islightet();
if(lighted){
System.out.println(vechicies.remove(0)+"is going!");
}
}
}
},
1,
1, TimeUnit.SECONDS);
具体的可以自己去查看文档,介绍的很详细。然后在自己去体会下。 |