package com.isoftstone.interview.traffic;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class Road {
List<String> verhicle=new ArrayList<String>();
private String name=null;
public Road( String name){
name=name;
//产生一个线程池
ExecutorService pool=Executors.newSingleThreadExecutor();
pool.execute(new Runnable(){
public void run(){
for(int i=0;i<1000;i++){
try{Thread.sleep((new Random().nextInt(10)+1)*1000);}
catch(InterruptedException e){
e.printStackTrace();
}
verhicle.add(Road.this.name+" "+i);
}
}
});
ScheduledExecutorService timer=Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable(){
public void run(){
if(verhicle.size()>0){
boolean lighted=Lamp.valueOf(Road.this.name).isLighted();
if(lighted){
System.out.println(verhicle.remove(0)+"is travelling");
}
}
}
},
1,
1,
TimeUnit.SECONDS);
}
}
我记得内部类是可以直接访问外类的private成员的,连private成员都可以直接访问我想其他权限的成员就更不用说了吧,可是为什么在这里内部类中调用外部类的name成员时要用Road.this.name这样的方式呢,还有就是说这行代码中间这this代表了啥?代表了内部类对象吗? |