Java Thread Future getExceptionFromNewThread(String threadName, Runnable target)

Here you can find the source of getExceptionFromNewThread(String threadName, Runnable target)

Description

get Exception From New Thread

License

Open Source License

Declaration

public static Throwable getExceptionFromNewThread(String threadName, Runnable target) 

Method Source Code


//package com.java2s;
// This software is released under the Apache License 2.0.

import java.util.concurrent.*;

public class Main {
    public static Throwable getExceptionFromNewThread(String threadName, Runnable target) {
        try {//  w  w w .  j ava  2s. c o m
            runInNewThread(threadName, target);
        } catch (Throwable throwable) {
            return throwable;
        }
        throw new AssertionError("Expected to throw an exception but did not throw anything");
    }

    public static void runInNewThread(String threadName, Runnable target) throws Throwable {
        FutureTask<Object> future = new FutureTask<Object>(target, null);
        new Thread(future, threadName).start();
        try {
            future.get();
        } catch (ExecutionException e) {
            throw e.getCause();
        }
    }
}

Related

  1. exceptionallyCompletedFuture( final Throwable e)
  2. exceptionallyCompletedFuture(final Throwable t)
  3. getAll(List> futures)
  4. getAll(List> futures)
  5. getAllDone(Collection futures)
  6. getFailedFuture(Throwable throwable)
  7. getFuture(Future future, String message)
  8. getMinIn(Map> responses)
  9. getResults(Iterable> futures)