本帖最后由 我叫MT 于 2014-3-3 15:22 编辑
- package com.itheima;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- public class Road {
- private List<String> vechicles = new ArrayList<String>();
-
- private String name = null;
- public Road(String name){
- this.name = name;
-
- ExecutorService pool = Executors.newSingleThreadExecutor();//车辆通行i为999俩车
- pool.execute(new Runnable(){
- @Override
- public void run() {
- try {
- Thread.sleep((new Random().nextInt(10)+1)*1000);//等待1到10秒
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- for(int i=1;i<1000;i++){
- vechicles.add(Road.this.name+"_"+i);
- }
- }
- });
- ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);//定时器
- timer.scheduleAtFixedRate(new Runnable(){
- public void run() {
- if(vechicles.size()>0){
- boolean lighted = true;
- if(lighted){
- System.out.println(vechicles.remove(0)+"-------");
- }
- }
- }
- },1,1,TimeUnit.SECONDS);
- }
- }
复制代码 vechicles.remove(0),这里删除0角标干嘛?最好用大白话说说汽车为什么会在这里remove(0)
|