- package com.isoftstone.interview.traffic;
- import java.util.*;
- public class Road {
-
- public static void main(String[] args){
- Road dx = new Road("东西");
- dx.addCar();
- dx.PrintCarsName();
- }
-
-
-
-
- private static int num=0;
- //名字
- private String name=null;
- //排队中的车辆
- private List<String> listCar = new ArrayList<String>();
-
- Road(String name){
- this.name=name;
- // addCar();
- }
-
- public void PrintCarsName(){
- for(String carName:listCar){
- System.out.println("嗷嗷");
- System.out.println(carName);
- }
- }
-
- //添加一辆车,可以以num代表车牌号,可以监控哦,这里简化直接用路线名字+车辆个数
- //public void addCar(int num){
- //public void addCar(){
-
- //这里先在构造函数随机添加车辆,后面专门建立一个类 做车辆的添加工作
- public void addCar(){
- new Thread(new Runnable(){
- @Override
- public void run() {
- // 一到十秒随机加入一辆车
-
- for(int i=1; i<10; i++){
- try {
- Thread.sleep((new Random().nextInt(10)+1)*1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- listCar.add(Road.this.name+":"+i);
- System.out.println("添加一辆车");
- }
-
- }
-
- }).start();
- }
- }
复制代码 为何dx.PrintCarsName();不执行???? |