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

CallablepowerTo(final double base, final double[] exponents)
power To
Callable c = new Callable() {
    public Object call() throws Exception {
        double[] result = new double[exponents.length];
        for (int i = 0; i < exponents.length; i++) {
            result[i] = Math.pow(base, exponents[i]);
        return result;
};
return c;
TrepeatedlyTry(Callable task, int maxRounds, long backoff)
repeatedly Try
List<Exception> list = new LinkedList<Exception>();
int round = 0;
while (round++ < maxRounds) {
    try {
        return task.call();
    } catch (Exception e) {
        list.add(e);
        try {
...
StringresolveCompositeKey(final String key, final Map props)
resolve Composite Key
String value = null;
final int comma = key.indexOf(',');
if (comma > -1) {
    if (comma > 0) {
        final String key1 = key.substring(0, comma);
        if (props != null) {
            Object o = props.get(key1);
            if (o instanceof Callable) {
...
ObjectresolveSingle(Object object)
resolve Single
try {
    return object instanceof Callable ? ((Callable<?>) object).call() : object;
} catch (Exception e) {
    throw new RuntimeException(e);
TrunAndRethrowRuntimeExceptionOnFailure(final Callable operation, final String exceptionMessage)
Runs the specified operation and re-throws any checked exception as RuntimeException.
try {
    return operation.call();
} catch (Exception e) {
    if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
    } else {
        throw new RuntimeException(exceptionMessage, e);
voidrunConcurrently(final Callable task)
Runs a task concurrently.
final ExecutorService service = Executors.newFixedThreadPool(5);
final List<Future<?>> futures = new ArrayList<Future<?>>();
for (int i = 0; i < 10; i++) {
    futures.add(service.submit(task));
for (final Future<?> future : futures) {
    future.get();
voidrunConcurrently(final Callable task)
Run the task concurrently 100 times.
runConcurrently(task, 100);
voidrunConcurrently(final Callable task, final int times)
Runs a task concurrently.
final ExecutorService service = Executors.newFixedThreadPool(5);
final List<Future<?>> futures = new ArrayList<Future<?>>();
for (int i = 0; i < times; i++) {
    futures.add(service.submit(task));
for (final Future<?> future : futures) {
    future.get();
VoidrunInBackground(final Callable callable)
run In Background
new Thread(new Runnable() {
    public void run() {
        try {
            callable.call();
        } catch (Exception e) {
            e.printStackTrace();
}).start();
return null;
voidrunTest(Callable test)
run Test
try {
    test.call();
} catch (RuntimeException e) {
    if ("org.autorefactor.util.UnhandledException".equals(e.getClass().getName())
            || "Unexpected exception".equals(e.getMessage())) {
        throw (Exception) e.getCause();
    throw e;
...