不要在ploteablefuture.supplyasync中更改当前时间

发布于 2025-01-21 16:11:46 字数 4248 浏览 1 评论 0原文

这里的摘要(Java 8)

LOGGER.info("==== MAIN: before_supplyAsync(), lastTimeSecLocal = {}, timeWindowSec = {}",
                                lastTimeSecLocal, timeWindowSec);
CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(() -> {
    long durationSecLocal = 0L;
    while (durationSecLocal < timeWindowSec) {
        long currentTimeSecLocal = System.currentTimeMillis() / 1000;
        durationSecLocal = currentTimeSecLocal - lastTimeSecLocal;
        LOGGER.info(
                    "\n\t\t>>>>>> supplyAsync: currentTimeSecLocal = {}, lastTimeSecLocal = {}, durationSecLocal = {}",
        currentTimeSecLocal, lastTimeSecLocal, durationSecLocal);
    }
        return true;
}); // end supplyAsync

和这里日志:

2022-04-15 11:59:41.179 INFO  [MyClass:http-nio-172.19.5.163-8091-exec-9] ==== MAIN: before_supplyAsync(), lastTimeSecLocal = 1650013181, timeWindowSec = 15
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0

为什么 CurrentTimesEclocal 不更改?

Here snippet (java 8)

LOGGER.info("==== MAIN: before_supplyAsync(), lastTimeSecLocal = {}, timeWindowSec = {}",
                                lastTimeSecLocal, timeWindowSec);
CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(() -> {
    long durationSecLocal = 0L;
    while (durationSecLocal < timeWindowSec) {
        long currentTimeSecLocal = System.currentTimeMillis() / 1000;
        durationSecLocal = currentTimeSecLocal - lastTimeSecLocal;
        LOGGER.info(
                    "\n\t\t>>>>>> supplyAsync: currentTimeSecLocal = {}, lastTimeSecLocal = {}, durationSecLocal = {}",
        currentTimeSecLocal, lastTimeSecLocal, durationSecLocal);
    }
        return true;
}); // end supplyAsync

And here logs:

2022-04-15 11:59:41.179 INFO  [MyClass:http-nio-172.19.5.163-8091-exec-9] ==== MAIN: before_supplyAsync(), lastTimeSecLocal = 1650013181, timeWindowSec = 15
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0
2022-04-15 11:59:41.183 INFO  [MyClass:ForkJoinPool.commonPool-worker-9] 
                >>>>>> supplyAsync: currentTimeSecLocal = 1650013181, lastTimeSecLocal = 1650013181, durationSecLocal = 0

Why currentTimeSecLocal not changed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萧瑟寒风 2025-01-28 16:11:46

CurrentTimesEclocal应该每秒更改,并且

我运行了此代码:

  public static void main(String[] args) throws Exception {

        long lastTimeSecLocal = System.currentTimeMillis() / 1000;
        long timeWindowSec = 10;

        log.info("==== MAIN: before_supplyAsync(), lastTimeSecLocal = {}, timeWindowSec = {}",
                lastTimeSecLocal, timeWindowSec);
        CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(() -> {
            long durationSecLocal = 0L;
            while (durationSecLocal < timeWindowSec) {
                long currentTimeSecLocal = System.currentTimeMillis() / 1000;
                durationSecLocal = currentTimeSecLocal - lastTimeSecLocal;
                log.info(
                        "\n\t\t>>>>>> supplyAsync: currentTimeSecLocal = {}, lastTimeSecLocal = {}, durationSecLocal = {}",
                        currentTimeSecLocal, lastTimeSecLocal, durationSecLocal);
            }
            return true;
        });
        
        completableFuture.get();
    }

日志是:

[cut]
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:38.000 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014198, lastTimeSecLocal = 1650014188, durationSecLocal = 10

currentTimeSecLocal should change every second, and it does

i runned this code:

  public static void main(String[] args) throws Exception {

        long lastTimeSecLocal = System.currentTimeMillis() / 1000;
        long timeWindowSec = 10;

        log.info("==== MAIN: before_supplyAsync(), lastTimeSecLocal = {}, timeWindowSec = {}",
                lastTimeSecLocal, timeWindowSec);
        CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(() -> {
            long durationSecLocal = 0L;
            while (durationSecLocal < timeWindowSec) {
                long currentTimeSecLocal = System.currentTimeMillis() / 1000;
                durationSecLocal = currentTimeSecLocal - lastTimeSecLocal;
                log.info(
                        "\n\t\t>>>>>> supplyAsync: currentTimeSecLocal = {}, lastTimeSecLocal = {}, durationSecLocal = {}",
                        currentTimeSecLocal, lastTimeSecLocal, durationSecLocal);
            }
            return true;
        });
        
        completableFuture.get();
    }

and the log is:

[cut]
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:37.999 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014197, lastTimeSecLocal = 1650014188, durationSecLocal = 9
11:16:38.000 [ForkJoinPool.commonPool-worker-19] INFO it.caensys.radbase.configuration.SecurityConfiguration - 
        >>>>>> supplyAsync: currentTimeSecLocal = 1650014198, lastTimeSecLocal = 1650014188, durationSecLocal = 10
陈甜 2025-01-28 16:11:46

这有帮助(我不明白为什么)

    LOGGER.info("==== MAIN: before_supplyAsync(), lastTimeSecLocal = {}, timeWindowSec = {}",
                                    lastTimeSecLocal, timeWindowSec);
    CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(() -> {
        long durationSecLocal = 0L;
        while (durationSecLocal < timeWindowSec) {
            long currentTimeSecLocal = System.currentTimeMillis() / 1000;
            durationSecLocal = currentTimeSecLocal - lastTimeSecLocal;
            LOGGER.info(
                        "\n\t\t>>>>>> supplyAsync: currentTimeSecLocal = {}, lastTimeSecLocal = {}, durationSecLocal = {}",
            currentTimeSecLocal, lastTimeSecLocal, durationSecLocal);
                try {
                        Thread.sleep(timeWindowSec * 100L);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
    }
        return true;
}); // end supplyAsync

This help (I don't understand why)

    LOGGER.info("==== MAIN: before_supplyAsync(), lastTimeSecLocal = {}, timeWindowSec = {}",
                                    lastTimeSecLocal, timeWindowSec);
    CompletableFuture<Boolean> completableFuture = CompletableFuture.supplyAsync(() -> {
        long durationSecLocal = 0L;
        while (durationSecLocal < timeWindowSec) {
            long currentTimeSecLocal = System.currentTimeMillis() / 1000;
            durationSecLocal = currentTimeSecLocal - lastTimeSecLocal;
            LOGGER.info(
                        "\n\t\t>>>>>> supplyAsync: currentTimeSecLocal = {}, lastTimeSecLocal = {}, durationSecLocal = {}",
            currentTimeSecLocal, lastTimeSecLocal, durationSecLocal);
                try {
                        Thread.sleep(timeWindowSec * 100L);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
    }
        return true;
}); // end supplyAsync
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文