File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed
core-utils/src/main/java/org/neo4j/gds/core/utils
core/src/main/java/org/neo4j/gds/core/concurrency Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change 2020package org .neo4j .gds .core .utils ;
2121
2222import java .time .Clock ;
23+ import java .util .concurrent .atomic .AtomicReference ;
24+ import java .util .function .Consumer ;
2325
2426public final class ClockService {
2527
2628 private static final Clock SYSTEM_CLOCK = Clock .systemUTC ();
2729
28- private static Clock CLOCK = SYSTEM_CLOCK ;
30+ private static final AtomicReference < Clock > CLOCK = new AtomicReference <>( SYSTEM_CLOCK ) ;
2931
3032 public static void setClock (Clock clock ) {
31- CLOCK = clock ;
33+ CLOCK . set ( clock ) ;
3234 }
3335
3436 public static Clock clock () {
35- return CLOCK ;
37+ return CLOCK . get () ;
3638 }
3739
3840 private ClockService () {}
41+
42+ public static <T extends Clock > void runWithClock (T clock , Consumer <T > runnable ) {
43+ Clock previousClock = CLOCK .getAndSet (clock );
44+ try {
45+ runnable .accept (clock );
46+ } finally {
47+ CLOCK .set (previousClock );
48+ }
49+ }
3950}
Original file line number Diff line number Diff line change 2929import java .util .concurrent .ForkJoinPool ;
3030import java .util .concurrent .FutureTask ;
3131import java .util .concurrent .RejectedExecutionHandler ;
32+ import java .util .concurrent .ScheduledExecutorService ;
3233import java .util .concurrent .ThreadFactory ;
3334import java .util .concurrent .ThreadPoolExecutor ;
3435import java .util .concurrent .TimeUnit ;
@@ -49,6 +50,10 @@ public static ExecutorService createSingleThreadPool(String threadPrefix) {
4950 return Executors .newSingleThreadExecutor (NamedThreadFactory .daemon (threadPrefix ));
5051 }
5152
53+ public static ScheduledExecutorService createSingleThreadScheduler (String threadPrefix ) {
54+ return Executors .newSingleThreadScheduledExecutor (NamedThreadFactory .daemon (threadPrefix ));
55+ }
56+
5257 static ExecutorService createThreadPool (int corePoolSize , int maxPoolSize ) {
5358 return createThreadPool (THREAD_NAME_PREFIX , corePoolSize , maxPoolSize );
5459 }
You can’t perform that action at this time.
0 commit comments