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

VexecuteUntilNonNullOrTimeout(final long timeoutMs, final long timeBetweenPollsMs, final Callable callable)
execute Until Non Null Or Timeout
final long started = new Date().getTime();
long elapsedTime = 0;
while (elapsedTime < timeoutMs) {
    final V value;
    try {
        value = callable.call();
    } catch (Exception e) {
        throw new RuntimeException(e);
...
TexecuteWithClassLoader(ClassLoader classLoader, Callable callable)
Execute the call within the given class loader...
ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
try {
    Thread.currentThread().setContextClassLoader(classLoader);
    return callable.call();
} finally {
    Thread.currentThread().setContextClassLoader(previousClassLoader);
voidexpectException(Callable callable, Class exception)
expect Exception
boolean thrown = false;
try {
    callable.call();
} catch (Throwable e) {
    assert e.getClass().equals(exception) : e.getClass().getName() + " is not " + exception.getName();
    thrown = true;
assert thrown : exception.getName() + " not received";
...
byte[]getBytes(final String string)
Returns the bytes with "UTF-8" encoding from the specified string.
return getBytes(string, "UTF-8");
FuturegetFuture(ExecutorService executorService, Callable callable)
Get a future from a given callable.
return executorService.submit(callable);
ObjectgetRandomValueForPrimitive(Class type)
get Random Value For Primitive
Callable<Object> c = p2r.get(type);
if (c == null)
    return null;
try {
    return c.call();
} catch (Exception e) {
    throw new RuntimeException(e);
List>invokeAll(Collection> tasks, ExecutorService executorService)
Invokes and waits all tasks using threadPool, avoiding thread starvation on the way (see "A Thread Pool Puzzler").
if (executorService == null) {
    for (Callable<T> task : tasks) {
        task.call();
    return null;
List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
boolean done = false;
...
voidinvokeBulkActions(Collection> tasks)
invoke Bulk Actions
invokeBulkActions(tasks, 20);
Callablemax(final double[] a, final double[] b)
max
if (a.length != b.length)
    throw new Exception("");
Callable c = new Callable() {
    public Object call() throws Exception {
        double[] result = new double[a.length];
        for (int i = 0; i < a.length; i++) {
            result[i] = Math.max(a[i], b[i]);
        return result;
};
return c;
CallableminScalar(final double[] a, final double scalar)
min Scalar
Callable c = new Callable() {
    public Object call() throws Exception {
        double[] result = new double[a.length];
        for (int i = 0; i < a.length; i++) {
            result[i] = Math.min(a[i], scalar);
        return result;
};
return c;