您的位置:首页 > 编程语言 > Java开发

Java基础入学考blog-11

2014-07-31 22:21 197 查看

张孝祥_7k月薪面试题破解之一_交通灯管理系统视频----笔记心得

面向对象设计把握一个重要的经验:谁拥有数据,谁就对外提供操作这些数据的方法

Stone--->StoneKnife 石头变成石刀,通过磨刀工厂。

StoneKnife =KnifeFactory.createKnife(stone){ ...;return StoneKnife};

内部类访问外部类成员变量,外部累成员变量必须是私有的,另一种方法:外部类名.this.成员变量名

随机数:new Random.nextInt(10)    10为0--9.

JDK1.5创建启动线程新特性

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);

    }    

   }

     });

创建定时移走车辆

ScheduledExecutorService timer =  Executors.newScheduledThreadPool(1);   //定时类  1是数量

  timer.scheduleAtFixedRate(    //四个参数,1:执行任务,2:首次执行时间。3,每个多长时间再次执行。4:时间数量单位

    new Runnable(){

     public void run(){

      if(vechicles.size()>0){

       boolean lighted = Lamp.valueOf(Road.this.name).isLighted();

       if(lighted){

        System.out.println(vechicles.remove(0) + " is traversing !");

       }

      }

      

     }

    },

    1,

    1,

    TimeUnit.SECONDS);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: