Package: JavaFXSafeRunner
JavaFXSafeRunner
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
lambda$runSafelyAndWait$0(AtomicReference, Callable, AtomicReference, CountDownLatch) |
|
|
|
|
|
||||||||||||||||||||
runSafelyAndWait(Callable) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
Coverage
1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.javafx.impl;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.concurrent.Callable;
30: import java.util.concurrent.CountDownLatch;
31: import java.util.concurrent.TimeUnit;
32: import java.util.concurrent.atomic.AtomicReference;
33: import javafx.application.Platform;
34: import lombok.SneakyThrows;
35: import lombok.experimental.UtilityClass;
36: import lombok.extern.slf4j.Slf4j;
37:
38: /***************************************************************************************************************************************************************
39: *
40: * @author Fabrizio Giudici
41: *
42: **************************************************************************************************************************************************************/
43: @UtilityClass @Slf4j
44: public final class JavaFXSafeRunner
45: {
46: private static final String P_TIMEOUT = DefaultNodeAndDelegate.class.getName() + ".initTimeout";
47: private static final int INITIALIZER_TIMEOUT = Integer.getInteger(P_TIMEOUT, 10);
48:
49: /***********************************************************************************************************************************************************
50: * {@return the value provided by the given {@link Callable}, running it in the JavaFX thread}.
51: * @param callable the callable
52: **********************************************************************************************************************************************************/
53: @SneakyThrows
54: public static <T> T runSafelyAndWait (@Nonnull final Callable<T> callable)
55: throws Exception
56: {
57:• if (Platform.isFxApplicationThread())
58: {
59: return callable.call();
60: }
61:
62: final var latch = new CountDownLatch(1);
63: final var result = new AtomicReference<T>();
64: final var exception = new AtomicReference<Throwable>();
65: final var timeoutMessage = String.format("Likely deadlock in the JavaFX Thread. If you need longer time with the debugger, set -D%s=<v> (current : %s)",
66: P_TIMEOUT, INITIALIZER_TIMEOUT);
67: Platform.runLater(() ->
68: {
69: try
70: {
71: result.set(callable.call());
72: }
73: catch (Throwable e)
74: {
75: exception.set(e);
76: }
77: finally
78: {
79: latch.countDown();
80: }
81: });
82:
83: try
84: {
85: log.debug("Waiting for JavaFX thread...");
86:
87:• if (!latch.await(INITIALIZER_TIMEOUT, TimeUnit.SECONDS))
88: {
89: throw new RuntimeException(timeoutMessage);
90: }
91: }
92: catch (InterruptedException e)
93: {
94: log.info("Interrupted");
95: Thread.currentThread().interrupt();
96: throw new RuntimeException(e);
97: }
98:
99:• if (exception.get() != null)
100: {
101: throw exception.get();
102: }
103:
104: return result.get();
105: }
106: }