A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 天山 中级黑马   /  2014-4-25 11:39  /  1107 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

我用银行系统那个例子修改
要等好久 才会出来 ,第一行,其它都正常这是怎么回事 大神能不能帮我改良下,怎么弄。

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

}



评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

4 个回复

倒序浏览
学得真快呀  !!!!
回复 使用道具 举报
zzkang0206 发表于 2014-4-25 16:00
学得真快呀  !!!!

0 0 被 版主这么一夸 我有一点点.. 轻飘飘了
回复 使用道具 举报
天山 发表于 2014-4-25 17:21
0 0 被 版主这么一夸 我有一点点.. 轻飘飘了

我也是刚开始学基础的
回复 使用道具 举报

                                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);
        }
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马