首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java中的多线程。同时倒计时和计时器

Java中的多线程。同时倒计时和计时器
EN

Stack Overflow用户
提问于 2018-06-12 06:46:31
回答 1查看 1.7K关注 0票数 0

当计时器和倒计时方法单独运行时,它们都可以工作。它们也作为两个单独的线程工作,但是线程序列的优先级导致倒计时和计时器(倒计时)不准确。

下面是输出示例: cookie 10 cookie 9 cookie 8秒1 cookie 7秒2秒3 cookie 6秒4 cookie 5秒5 cookie 4 cookie 3秒6 cookie 2秒7 cookie 1秒8秒9秒10秒11秒12秒13秒14秒15秒16秒17秒18秒19

一旦cookie计时器完成,计时器应该继续,但是,当计时器和倒计时都处于活动状态时,线程应该以秒为单位跟踪时间,而不会失去准确性。目前,我依靠控制台来显示此计数,但是我将在图形界面中对此进行编程,以向用户显示倒计时和计时器。也许线程的执行需要在它们之间轮换,以便计时器均匀地进行,此外,它们应该同步,这样一个线程就不能在没有另一个的情况下继续执行。关于实现的任何提示。谢谢。

公共类控制台{

代码语言:javascript
运行
复制
     long lastTime;
     boolean counting = true;
     boolean cCounting = true;
     long seconds = 0;
     long delta = 0;
     volatile int startNumber = (int) Level.cookieTime / 1000;
    Thread countDown;
    Thread time;

    public Console() {

        counting = Game.enter;

        cookieCountDown();
        timer();
        lastTime = System.currentTimeMillis();
    }

    public void timer() {

        time = new Thread(new Runnable() {
            @Override
            public void run() {
                seconds = 0;


                while (true) {
                    try {
                        Thread.sleep(0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    counting = Game.enter;

                    while (counting) {

                        long now = System.currentTimeMillis();
                        delta = now - lastTime;

                        if (delta >= 1000) {

                            delta = 0;
                            lastTime = System.currentTimeMillis();
                            System.out.println("seconds " + seconds); //Print seconds
                            seconds++;

                            if (!counting){
                                System.out.println("stoped"  + counting);
                                //time.stop();
                              //  return;
                            }
                        }
                    }
                }
            }

        });
        time.start();

    }

    public void cookieCountDown() {
        countDown = new Thread(new Runnable() {
            @Override
            public void run() {
                Player.cCounting = true;

                while (startNumber != 0) {
                    startNumber = (int) Level.cookieTime / 1000;
                    cCounting = Game.enter;

                    while (startNumber > 0 && cCounting) {
                        long now = System.currentTimeMillis();
                        delta = now - lastTime;

                        if (delta >= 1000) {
                            delta = 0;
                            lastTime = System.currentTimeMillis();
                            System.out.println("cookie " + startNumber);// print countdown;
                            startNumber--;

                            if (!Player.cCounting) {
                                Player.cCounting = true;
                                return;
                            }
                        }
                    }
                }
            }
        });
        countDown.start();

        if (startNumber == 0 || !Player.cCounting) {

            Player.cCounting = true;
            startNumber = (int) Level.cookieTime / 1000;

        }
    }


    public void setCounting(boolean counting) {
        this.counting = counting;
    }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50807048

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档