Example usage for java.util.concurrent Callable call

List of usage examples for java.util.concurrent Callable call

Introduction

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

Prototype

V call() throws Exception;

Source Link

Document

Computes a result, or throws an exception if unable to do so.

Usage

From source file:Main.java

public static void main(String args[]) {
    Callable<String> c = () -> "Hello you!";
    try {/*from   w  w w. j a va 2s.co m*/
        System.out.println(c.call());
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

From source file:Main.java

public static void main(String... args) {
    Callable<UUID> callable = UUID::randomUUID;
    try {/* w  ww.j a v a  2s  . co m*/
        System.out.println(callable.call());
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:org.apache.blur.mapreduce.lib.CsvBlurDriver.java

public static void main(String... args) throws Exception {
    Configuration configuration = new Configuration();
    String[] otherArgs = new GenericOptionsParser(configuration, args).getRemainingArgs();
    AtomicReference<Callable<Void>> ref = new AtomicReference<Callable<Void>>();
    Job job = setupJob(configuration, new ControllerPool() {
        @Override//ww w . ja  v a 2s  .  c o  m
        public Iface getClient(String controllerConnectionStr) {
            return BlurClient.getClient(controllerConnectionStr);
        }
    }, ref, otherArgs);
    if (job == null) {
        System.exit(1);
    }
    boolean waitForCompletion = job.waitForCompletion(true);
    if (waitForCompletion) {
        Callable<Void> callable = ref.get();
        if (callable != null) {
            callable.call();
        }
    }
    System.exit(waitForCompletion ? 0 : 1);
}

From source file:Main.java

public static <T> T performLocked(Lock lock, Callable<T> action) {
    lock.lock();//from w w  w .  j  a v a2s .  com
    try {
        return action.call();
    } catch (RuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new RuntimeException(e);
    } finally {
        lock.unlock();
    }
}

From source file:Main.java

public static <T> Future<T> timeout(long millis, Callable<T> callable) {
    return POOL.submit(() -> {
        sleep(millis);//from   w ww.ja v  a2s .  co  m
        return callable.call();
    });
}

From source file:co.cask.cdap.client.rest.TestUtils.java

public static void verifyException(Class<? extends RuntimeException> expectedException,
        Callable<Void> callable) {
    try {/*from  w  w w.j  a  v  a 2 s . c o m*/
        callable.call();
        Assert.fail("Expected exception type: " + expectedException.getName());
    } catch (Exception e) {
        Assert.assertEquals(expectedException, e.getClass());
    }
}

From source file:Main.java

public static <T> void invokeLater(Callable<T> callable, JSObject callback) {
    SwingUtilities.invokeLater(() -> {
        try {/*from  w  w w. j av  a2 s.  c  om*/
            final T result = callable.call();
            if (callback != null) {
                Platform.runLater(() -> callback.call("call", null, result));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}

From source file:org.polymap.core.runtime.LockUtils.java

/**
 * Executes the given callable inside read lock of the given lock. The call()
 * method may aquire the write lock during execution. Both locks are released
 * after this method returns./*from ww w .  j a v  a 2  s  .  c o m*/
 * 
 * @param <V>
 * @param rwLock
 * @param task
 * @return The result of the given {@link #task}.
 * @throws Exception The exception from the {@link #task}.
 */
public static <V> V withReadLock(ReentrantReadWriteLock rwLock, Callable<V> task) throws Exception {
    try {
        rwLock.readLock().lock();

        return task.call();
    } finally {
        if (rwLock.writeLock().isHeldByCurrentThread()) {
            rwLock.readLock().lock();
            rwLock.writeLock().unlock();
        }
        rwLock.readLock().unlock();
    }
}

From source file:ec.nbdemetra.ui.demo.ComponentsDemo.java

private static Component create(Callable<? extends Component> factory) {
    try {//from  www .j ava  2 s  .  c  o m
        return factory.call();
    } catch (Exception ex) {
        ExceptionPanel panel = new ExceptionPanel();
        panel.setException(ex);
        return panel;
    }
}

From source file:com.aerospike.delivery.db.base.Database.java

public static boolean withReadLock(ReentrantReadWriteLock lock, Callable<Boolean> action) {
    lock.readLock().lock();/* w ww.ja v a  2s  .com*/
    try {
        return action.call();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    } finally {
        lock.readLock().unlock();
    }
}