List of usage examples for javafx.concurrent Worker getValue
public V getValue();
From source file:Main.java
/** * The given consumer will be called once the worker is finished. The result of the worker * will be passed to the consumer.// ww w . j av a2s .c o m * @param worker the worker * @param consumer the consumer * @param <T> the resukt type of the worker */ public static <T> void then(Worker<T> worker, Consumer<T> consumer) { ReadOnlyBooleanProperty doneProperty = createIsDoneProperty(worker); ChangeListener<Boolean> listener = (o, oldValue, newValue) -> { if (newValue) { consumer.accept(worker.getValue()); } }; doneProperty.addListener(listener); }
From source file:Main.java
/** * This methods blocks until the worker is done and returns the result value of the worker. * If the worker was canceled an exception will be thrown. * * @param worker The worker//from w w w . j a va 2s. com * @param <T> result type of the worker * @return the result * @throws InterruptedException if the worker was canceled */ public static <T> T waitFor(Worker<T> worker) throws InterruptedException { Lock lock = new ReentrantLock(); Condition condition = lock.newCondition(); lock.lock(); try { ReadOnlyBooleanProperty doneProperty = createIsDoneProperty(worker); if (doneProperty.get()) { return worker.getValue(); } else { doneProperty.addListener(e -> { boolean locked = lock.tryLock(); if (locked) { try { condition.signal(); } finally { lock.unlock(); } } else { throw new RuntimeException("Concurreny Error"); } }); condition.await(); return worker.getValue(); } } finally { lock.unlock(); } }