Example usage for java.util.concurrent FutureTask get

List of usage examples for java.util.concurrent FutureTask get

Introduction

In this page you can find the example usage for java.util.concurrent FutureTask get.

Prototype

public V get() throws InterruptedException, ExecutionException 

Source Link

Usage

From source file:org.kontalk.view.View.java

static <T> Optional<T> invokeAndWait(Callable<T> callable) {
    try {//  w  w w.  j a  va2  s .  c o m
        FutureTask<T> task = new FutureTask<>(callable);
        SwingUtilities.invokeLater(task);
        // blocking
        return Optional.of(task.get());
    } catch (ExecutionException | InterruptedException ex) {
        LOGGER.log(Level.WARNING, "can't execute task", ex);
    }
    return Optional.empty();
}

From source file:MainClass.java

public void invokeAneWait(Runnable r) throws InterruptedException, ExecutionException {
    FutureTask task = new FutureTask(r, null);
    tpe.execute(task);/*from  w w w. jav a2 s  .c  o m*/
    task.get();
}

From source file:net.dryuf.concurrent.benchmark.NoListenerAsyncBenchmark.java

@Benchmark
@Warmup(iterations = WARMUP_ITERATIONS)//  ww w . jav  a 2s  . co m
@Measurement(iterations = 2, batchSize = 1)
@Fork(warmups = 1, value = 1)
public void benchmarkJdk() throws Exception {
    FutureTask[] array = BenchmarkSupport.populateJdkFutureArray(COUNT);
    BenchmarkSupport.threadedRunFutures(array);
    for (FutureTask<Integer> f : array) {
        f.get();
    }
}

From source file:net.stuxcrystal.airblock.canary.CanaryServerBackend.java

@Override
public <R> R callInMainThread(Callable<R> callable) throws Throwable {
    if (this.mTID == -1)
        throw new IllegalStateException("We do not know the main-thread id yet.");

    if (Thread.currentThread().getId() == this.mTID) {
        return callable.call();
    }//from w w w. j a  v  a2  s.c o m

    FutureTask<R> ft = new FutureTask<R>(callable);
    this.runLater(ft);
    return ft.get();
}

From source file:net.dryuf.concurrent.benchmark.DoublePreListenerBenchmark.java

@Benchmark
@Warmup(iterations = WARMUP_ITERATIONS)//from  w  ww . ja  v  a2s. c om
@Measurement(iterations = 1, batchSize = 1)
@Fork(warmups = 1, value = 1)
public void benchmarkJdk() throws Exception {
    for (long i = 0; i < COUNT; ++i) {
        FutureTask<Integer> future = new FutureTask<Integer>(() -> {
            return 0;
        });
        future.run();
        future.get();
    }
}

From source file:net.dryuf.concurrent.benchmark.MixedListenerBenchmark.java

@Benchmark
@Warmup(iterations = WARMUP_ITERATIONS)//w  ww . j av a 2 s . c om
@Measurement(iterations = 2, batchSize = 1)
@Fork(warmups = 1, value = 1)
public void benchmarkJdk() throws Exception {
    for (long i = 0; i < COUNT; ++i) {
        FutureTask<Integer> future = new FutureTask<Integer>(() -> {
            return 0;
        });
        future.run();
        future.get();
    }
}

From source file:com.google.code.jconfig.factory.ConfigurationPluginFactory.java

/**
 * <p>//w w  w .  j  av a 2 s.c o m
 *    Returns a plugin instance of type <em>classname</em>.
 * </p>
 * 
 * <p>
 *    If the implementation class use the {@link Cacheable} annotation, it
 *    will be cached for future reuse otherwise a new instance will be
 *    created at every request.
 * </p>
 * 
 * @param classname the full name of an instance of
 *                  {@link IConfigurationPlugin}.
 * @return the plugin instance.
 * @throws PluginInstantiationException
 */
public static IConfigurationPlugin<?> getPlugin(final String classname) throws PluginInstantiationException {

    try {
        if (ClassUtils.getClass(classname).getAnnotation(Cacheable.class) == null) {
            logger.debug("Plugin <" + classname + "> is not cacheable. Creating a new one.");
            return (IConfigurationPlugin<?>) Class.forName(classname).newInstance();
        }
    } catch (Exception e) {
        throw new PluginInstantiationException(e.getMessage(), e);
    }

    // cacheable plugin
    FutureTask<IConfigurationPlugin<?>> theTask = cache.get(classname);
    if (theTask == null) {
        logger.debug("No plugin of class <" + classname + "> available. Creatine a new one.");
        Callable<IConfigurationPlugin<?>> eval = new Callable<IConfigurationPlugin<?>>() {

            public IConfigurationPlugin<?> call() throws Exception {
                IConfigurationPlugin<?> plugin = (IConfigurationPlugin<?>) Class.forName(classname)
                        .newInstance();
                return plugin;
            }
        };

        FutureTask<IConfigurationPlugin<?>> thePluginExec = new FutureTask<IConfigurationPlugin<?>>(eval);
        theTask = cache.putIfAbsent(classname, thePluginExec);
        if (theTask == null) {
            theTask = thePluginExec;
            theTask.run();
        }
        logger.debug("New plugin of class <" + classname + "> ready to be used and cached.");
    } else {
        logger.debug("Plugin of class <" + classname + "> available in cache. Using the cached one.");
    }

    try {
        return theTask.get();
    } catch (Exception e) {
        throw new PluginInstantiationException(e.getMessage(), e);
    } finally {
        logger.debug("plugin cache size: " + cache.size() + " - cache detail: " + cache);
    }
}

From source file:net.dryuf.concurrent.benchmark.SinglePostListenerAsyncBenchmark.java

@Benchmark
@Warmup(iterations = WARMUP_ITERATIONS)//www. j  a  va  2s .  c om
@Measurement(iterations = 2, batchSize = 1)
@Fork(warmups = 1, value = 1)
public void benchmarkJdk() throws Exception {
    FutureTask<Integer>[] array = BenchmarkSupport.populateJdkFutureArray(COUNT);
    for (FutureTask<Integer> f : array) {
        // nothing to add, no listenable
    }
    BenchmarkSupport.threadedRunFutures(array);
    for (FutureTask<Integer> f : array) {
        f.get();
    }
}

From source file:com.github.vatbub.tictactoe.view.AnimationThreadPoolExecutor.java

/**
 * @throws RejectedExecutionException {@inheritDoc}
 * @throws NullPointerException       {@inheritDoc}
 *//*from   w  w w . j  a  v  a2s.c om*/
@NotNull
@Override
public <T> Future<T> submit(Callable<T> task) {
    Callable<T> effectiveTask = () -> {
        FutureTask<T> effectiveCall = new FutureTask<>(task);
        Platform.runLater(effectiveCall);
        return effectiveCall.get();
    };
    return super.schedule(effectiveTask, 0, NANOSECONDS);
}

From source file:net.dryuf.concurrent.benchmark.DoublePreListenerAsyncBenchmark.java

@Benchmark
@Warmup(iterations = WARMUP_ITERATIONS)// ww w  .j av a 2s.c o m
@Measurement(iterations = 2, batchSize = 1)
@Fork(warmups = 1, value = 1)
public void benchmarkJdk() throws Exception {
    FutureTask[] array = BenchmarkSupport.populateJdkFutureArray(COUNT);
    for (FutureTask<Integer> f : array) {
        // nothing to add, no listenable
    }
    BenchmarkSupport.threadedRunFutures(array);
    for (FutureTask<Integer> f : array) {
        f.get();
    }
}