本帖最后由 昝文萌 于 2013-9-22 07:44 编辑
在Road类里面有一段开启线程增加车辆的代码- 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();
- pool.execute(new Runnable(){
- public void run(){
- for(int i=1;i<1000;i++){
- try {
- Thread.sleep((new Random().nextInt(10) + 1) * 1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- vechicles.add(Road.this.name + "_" + i);
- }
复制代码 vechicles.add(Road.this.name + "_" + i);//这里为什麽要用Road.this.name ,内部类不是直接可以访问成员变量name吗?为什麽还要加Road.this
直接写成vechicles.add(name + "_" + i)不行吗?
|