黑马程序员技术交流社区

标题: 红绿灯切换显示,怎么运行开始很慢 [打印本页]

作者: 天山    时间: 2014-4-25 11:39
标题: 红绿灯切换显示,怎么运行开始很慢
我用银行系统那个例子修改
要等好久 才会出来 ,第一行,其它都正常这是怎么回事 大神能不能帮我改良下,怎么弄。

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

}




作者: zzkang0206    时间: 2014-4-25 16:00
学得真快呀  !!!!
作者: 天山    时间: 2014-4-25 17:21
zzkang0206 发表于 2014-4-25 16:00
学得真快呀  !!!!

0 0 被 版主这么一夸 我有一点点.. 轻飘飘了
作者: zzkang0206    时间: 2014-4-25 17:33
天山 发表于 2014-4-25 17:21
0 0 被 版主这么一夸 我有一点点.. 轻飘飘了

我也是刚开始学基础的
作者: 执笔梦    时间: 2014-4-26 15:19

                                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,//你这里设置为10了啊,首次执行的延迟时间
,你改为2试下

                                10, // 连环爆破,一秒一次 连续的 4个参数timer.scheduleAtFixedRate
                                TimeUnit.SECONDS);
        }





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2