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