Java Utililty Methods Thread Callable

List of utility methods to do Thread Callable

Description

The list of methods to do Thread Callable are organized into topic(s).

Method

RrunWithTimeout(long millisTimeout, Callable callable)
Runs and blocking waits for the given callable to finish for the given time.
ExecutorService singleThreadExecutor = Executors.newFixedThreadPool(1);
Future<R> future = singleThreadExecutor.submit(callable);
try {
    return future.get(millisTimeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
} finally {
    singleThreadExecutor.shutdown();
return null;
CallableshuffleRow(final double[] data)
shuffle Row
return new Callable() {
    int len = data.length;
    public Object call() throws Exception {
        for (int i = 0; i < data.length; i++) {
            double temp = data[i];
            int index = random.nextInt(len);
            data[i] = data[index];
            data[index] = temp;
...
TsubmitAndWait(ListeningExecutorService service, Callable ca)
submit And Wait
ListenableFuture<T> lf = service.submit(ca);
return lf.get();
ListsubmitManyAndWait(ListeningExecutorService service, Iterable> cas, FutureCallback fca)
submit Many And Wait
List<ListenableFuture<T>> lfs = new ArrayList<>();
for (Callable<T> ca : cas) {
    lfs.add(service.submit(ca));
return Futures.successfulAsList(lfs).get();
Callablesum(final double[] a)
sum
Callable c = new Callable() {
    public Object call() throws Exception {
        Double max = null;
        double[] result = new double[a.length];
        max = a[0];
        for (int i = 1; i < a.length; i++) {
            max += a[i];
        return max;
};
return c;
CallablesumOfSquares(final double[] data)
sum Of Squares
return new Callable() {
    int len = data.length;
    double sum = 0;
    public Object call() throws Exception {
        for (int i = 0; i < data.length; i++) {
            double temp = data[i];
            sum += temp * temp;
        return sum;
};
CallablewaitFor(final Process process, final CyclicBarrier onStart)
Waits for a process to terminate.
return new Callable<Integer>() {
    public Integer call() throws InterruptedException, BrokenBarrierException {
        onStart.await();
        return process.waitFor();
};
voidwaitFor(Object o, long ms, Callable c)
Wait for async work to finish.
synchronized (o) {
    final long before = System.currentTimeMillis();
    final long step = ms / 10;
    long d = ms;
    while (!c.call() && (0 < d)) {
        o.wait(0 < step ? step : 1);
        d = ms - (System.currentTimeMillis() - before);
voidwaitForCondition(Callable condition, Callable callback, int seconds)
wait For Condition
try {
    while (seconds-- > 0) {
        if (condition.call()) {
            return;
        if (callback != null) {
            callback.call();
        Thread.sleep(500);
} catch (InterruptedException ee) {
    ee.printStackTrace();
    throw ee;
} catch (Exception ee) {
    throw ee;
throw new Exception("condition not met in time!");