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 20
20
package org .neo4j .gds .core .utils ;
21
21
22
22
import java .time .Clock ;
23
+ import java .util .concurrent .atomic .AtomicReference ;
24
+ import java .util .function .Consumer ;
23
25
24
26
public final class ClockService {
25
27
26
28
private static final Clock SYSTEM_CLOCK = Clock .systemUTC ();
27
29
28
- private static Clock CLOCK = SYSTEM_CLOCK ;
30
+ private static final AtomicReference < Clock > CLOCK = new AtomicReference <>( SYSTEM_CLOCK ) ;
29
31
30
32
public static void setClock (Clock clock ) {
31
- CLOCK = clock ;
33
+ CLOCK . set ( clock ) ;
32
34
}
33
35
34
36
public static Clock clock () {
35
- return CLOCK ;
37
+ return CLOCK . get () ;
36
38
}
37
39
38
40
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
+ }
39
50
}
Original file line number Diff line number Diff line change 29
29
import java .util .concurrent .ForkJoinPool ;
30
30
import java .util .concurrent .FutureTask ;
31
31
import java .util .concurrent .RejectedExecutionHandler ;
32
+ import java .util .concurrent .ScheduledExecutorService ;
32
33
import java .util .concurrent .ThreadFactory ;
33
34
import java .util .concurrent .ThreadPoolExecutor ;
34
35
import java .util .concurrent .TimeUnit ;
@@ -49,6 +50,10 @@ public static ExecutorService createSingleThreadPool(String threadPrefix) {
49
50
return Executors .newSingleThreadExecutor (NamedThreadFactory .daemon (threadPrefix ));
50
51
}
51
52
53
+ public static ScheduledExecutorService createSingleThreadScheduler (String threadPrefix ) {
54
+ return Executors .newSingleThreadScheduledExecutor (NamedThreadFactory .daemon (threadPrefix ));
55
+ }
56
+
52
57
static ExecutorService createThreadPool (int corePoolSize , int maxPoolSize ) {
53
58
return createThreadPool (THREAD_NAME_PREFIX , corePoolSize , maxPoolSize );
54
59
}
You can’t perform that action at this time.
0 commit comments