'Not change current time in CompletableFuture.supplyAsync
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?
Solution 1:[1]
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
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Francesco Rogo |
| Solution 2 | Alexei |
