我用银行系统那个例子修改
要等好久 才会出来 ,第一行,其它都正常这是怎么回事 大神能不能帮我改良下,怎么弄。
package com.itheima;
public enum test_4 {
RED("GREEN"),GREEN("YELLOW"),YELLOW("RED");
private String nextlamp;
//定义带String 参数构造函数
private test_4(String nextlamp)
{
this.nextlamp=nextlamp;
}
public test_4 nextLamp(){
//返回下个灯
return test_4.valueOf(nextlamp);
}
public void light(){
System.out.println(this.name()+"");
}
public void black(){
System.out.println(this.name()+"");
}
}
package com.itheima;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class lamp_control {
private test_4 currentlamp;
public lamp_control() {
ScheduledExecutorService timer = Executors.newScheduledThreadPool(1);
timer.scheduleAtFixedRate(new Runnable() {
public void run() {
while (true) {
System.out.println("要_换灯了");
currentlamp = test_4.GREEN;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentlamp.light();
System.out.println("绿灯了,此时不冲更待何时?");
currentlamp = test_4.YELLOW;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentlamp.light();
System.out.println("黄灯了,赶着去投胎?");
currentlamp = test_4.RED;
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentlamp.light();
System.out.println("红灯了,珍惜生命");
}
}
}, 10, // 过了2 把当用的灯变黑
10, // 连环爆破,一秒一次 连续的 4个参数timer.scheduleAtFixedRate
TimeUnit.SECONDS);
}
public static void main(String[] args) {
lamp_control a = new lamp_control();
}
}
|