Java基础入学考blog-11

张孝祥_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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值