Example usage for java.util.concurrent FutureTask FutureTask

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

Introduction

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

Prototype

public FutureTask(Callable<V> callable) 

Source Link

Document

Creates a FutureTask that will, upon running, execute the given Callable .

Usage

From source file:Main.java

public static void blockUntilConnected(final SocketChannel channel, long timeout) throws IOException {
    ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, timeout, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<Runnable>());
    FutureTask<Boolean> future = new FutureTask<Boolean>(new Callable<Boolean>() {
        public Boolean call() {
            while (!channel.isConnected()) {
                try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }//from w  ww  . j ava2  s. com
            }
            return true;
        }
    });
    executor.execute(future);

    try {
        future.get(timeout, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        channel.close();
        throw new IOException(e);
    }
}

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

public static FutureTask<Integer>[] populateJdkFutureArray(int count) {
    Callable<Integer> func = () -> {
        return 0;
    };//from  w ww. ja  v  a  2 s .  c o  m
    FutureTask<Integer>[] array = new FutureTask[count];
    for (int i = 0; i < count; ++i) {
        array[i] = new FutureTask<Integer>(func);
    }
    return array;
}

From source file:Main.java

public static <T> T invokeAndWait(Callable<T> callable)
        throws TimeoutException, ExecutionException, InterruptedException {
    //blocks until future returns
    FutureTask<T> task = new FutureTask<>(callable);
    SwingUtilities.invokeLater(task);
    return task.get();
}

From source file:Main.java

/**
 * Run the supplied Callable on the main thread, The method will block until the Callable
 * completes./*  w ww.  j a  v  a  2 s  . c o  m*/
 *
 * @param c The Callable to run
 * @return The result of the callable
 * @throws ExecutionException c's exception
 */
public static <T> T runOnUiThreadBlocking(Callable<T> c) throws ExecutionException {
    FutureTask<T> task = new FutureTask<T>(c);
    runOnUiThread(task);
    try {
        return task.get();
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted waiting for callable", e);
    }
}

From source file:Main.java

/**
 * Submits a value-returning task for execution on the EDT and
 * returns a Future representing the pending results of the task.
 *
 * @param task the task to submit//from w w  w. j av a2s.  c  om
 * @return a Future representing pending completion of the task
 * @throws NullPointerException if the task is null
 */
public static <V> Future<V> submit(Callable<V> task) {
    if (task == null) {
        throw new NullPointerException();
    }
    FutureTask<V> future = new FutureTask<V>(task);
    execute(future);
    return future;
}

From source file:com.vaadin.tools.ReportUsage.java

public static FutureTask<Void> checkForUpdatesInBackgroundThread() {
    FutureTask<Void> task = new FutureTask<>(new Callable<Void>() {
        @Override/*from w w w. j av a 2s  . co  m*/
        public Void call() throws Exception {
            ReportUsage.report();
            return null;
        }
    });
    Thread checkerThread = new Thread(task, "Vaadin Update Checker");
    checkerThread.setDaemon(true);
    checkerThread.start();
    return task;
}

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

@Benchmark
@Warmup(iterations = WARMUP_ITERATIONS)//ww  w.j a  va2 s.  c  o m
@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();
    }
}

From source file:com.wk.wechat4j.base.util.SettableFuture.java

public SettableFuture() {
    this.settableTask = new SettableTask<T>();
    this.futureTask = new FutureTask<T>(this.settableTask);
}

From source file:br.com.sicoob.cro.cop.batch.step.StepTaskletExecutor.java

public void start() throws Exception {
    ConstructorUtils.invokeConstructor(TaskletInjector.class, this.step).inject();
    TaskletExecutor taskletExecutor = new TaskletExecutor(this.step);
    this.task = new FutureTask(taskletExecutor);
    this.service.executeTask(task);
}

From source file:org.sipfoundry.sipxconfig.admin.configdiag.ConfigurationDiagnosticContextImpl.java

/**
 * Similar to ExecutorService.invokeAll but this method is not going to block. You can check
 * the progress later by analysing returned futures.
 *//*from   w  w  w . j  a  v a 2s. c o  m*/
static <T> List<Future<T>> scheduleAll(Executor executor, Collection<? extends Callable<T>> tasks)
        throws InterruptedException {
    List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
    for (Callable<T> t : tasks) {
        FutureTask<T> f = new FutureTask<T>(t);
        futures.add(f);
        executor.execute(f);
    }
    return futures;
}