Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的倒计时只下降了1,没有更多,为什么?_Java_Countdown - Fatal编程技术网

Java 我的倒计时只下降了1,没有更多,为什么?

Java 我的倒计时只下降了1,没有更多,为什么?,java,countdown,Java,Countdown,因此,我想在服务类中为用户输入的日期创建一个倒计时。现在,我对倒计时有问题。看起来不管是什么,它只减1秒,不进一步。下面是代码: 倒计时的逻辑 package com.example.service; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class CounterService { LocalDateTime dateTime; public Lo

因此,我想在服务类中为用户输入的日期创建一个倒计时。现在,我对倒计时有问题。看起来不管是什么,它只减1秒,不进一步。下面是代码:

倒计时的逻辑

package com.example.service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class CounterService {
    LocalDateTime dateTime;

    public LocalDateTime getDateTime(){
        return dateTime;
    }

    public void setDateTime(LocalDateTime dateTime) {
        this.dateTime = dateTime;
    }

    public LocalDateTime parseDate(String eventDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        dateTime = LocalDateTime.parse(eventDate, formatter);
        return dateTime;
    }


    public static void main(String[] args) {

        CounterService cs = new CounterService();
        LocalDateTime a = LocalDateTime.of(2021,11,11,21,43,44);
        cs.setDateTime(a);

        Runnable r = new Counter(cs);
        new Thread(r).start();

    }
}
还有Runnable

package com.example.service;

import java.time.LocalDateTime;

public class Counter implements Runnable {
    CounterService counter;

    public Counter(CounterService cs) {
        this.counter = cs;
    }

    public void setCounter(CounterService cs) {
        this.counter = cs;
    }

    public void run() {
        LocalDateTime countFromThisDate = counter.getDateTime();
        try {
            boolean x = true;
            while (x) {
                LocalDateTime substracting = countFromThisDate.minusSeconds(1);
                Thread.sleep(1000);
                System.out.println(substracting);
            }
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}
另外,如何在计数器工作后添加此计数器,使其动态刷新并每秒逐个减?

LocalDateTime是不可变的

countFromThisDate.minusSeconds1返回一个新实例,它不会更新调用它的实例。您需要将结果分配回某个位置,例如通过调用服务上的setDateTime

这和字符串的工作原理一样


该服务还需要保证线程安全。为此,您可能应该将减法逻辑移到服务本身中,这样它就可以成为原子的,而不是get/subtract/set。

在这里说明一个实现缺陷-thread.sleep不能保证线程在经过的间隔之后运行,它只是使线程状态在间隔后准备好由cpu运行。相反,您应该使用系统时间来获取计数器启动后经过的时间

-更新-

你的会务服务稍有改变就可以了。除倒计时时间外,反服务还将保留开始时间。代码如下所示-

class CounterService {      

    private LocalDateTime dateTime;

    private LocalDateTime startTime;

    public CounterService(LocalDateTime dateTime, LocalDateTime startTime) {
        this.dateTime = dateTime;
        this.startTime = startTime;
    }

    public LocalDateTime getCounterTime() {
        return dateTime.minusSeconds(startTime.until(LocalDateTime.now(), ChronoUnit.SECONDS));
    }

    public static void main (String[] args) throws java.lang.Exception {
        LocalDateTime a = LocalDateTime.of(2021,11,11,21,43,44);
        LocalDateTime startTime = LocalDateTime.now();

        final CounterService counterService = new CounterService(a, startTime);

        new Thread(() -> {
            while (true) {
                System.out.println(counterService.getCounterTime());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

谢谢你的回复,你能给我一个与我的情况相关的代码的例子吗?好的,这似乎是一个很好的方法。顺便说一句,我需要做的是在这之后,发送倒计时计时器给用户,你也能帮我吗?事实上,我没有得到你的用例。问题是-为什么您的倒计时计时器返回减去的日期时间而不是相对时间,直到日期时间像xx天xx小时xx分钟。。。现在倒计时可以正常工作了,但我需要找到一种方法在servlet jsp中使用它,并在我看到的Webgeok中显示它。我这样做了,我得到了同样的问题。所以我添加了counter.setDateTimeSubstrating;就在Thread.sleep1000之前;它仍然只进行了1秒的减法运算,然后在相同的结果上循环。你能帮我写一些代码吗?好的,看来我只需要移动一行代码就可以了。非常感谢!