use of java.util.concurrent.FutureTask in project indy by Commonjava.
the class ThreadDumper method timeoutRule.
public static TestRule timeoutRule(int timeout, TimeUnit units) {
return (base, description) -> new Statement() {
public void evaluate() throws Throwable {
System.out.printf("Setting up timeout: %d %s to wrap: %s\n", timeout, units, base);
AtomicReference<Throwable> error = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
FutureTask<Void> task = new FutureTask<>(() -> {
try {
latch.countDown();
base.evaluate();
} catch (Throwable t) {
error.set(t);
}
return null;
});
ThreadGroup tg = new ThreadGroup("Test Timeout Group");
Thread t = new Thread(tg, task, "Test Timeout Thread");
t.setDaemon(true);
t.start();
try {
System.out.println("Waiting for test to start.");
latch.await();
} catch (InterruptedException e) {
error.set(e);
}
if (error.get() == null) {
try {
System.out.println("Waiting for test to complete (or timeout)");
task.get(timeout, units);
} catch (InterruptedException e) {
error.set(e);
} catch (ExecutionException e) {
error.set(e.getCause());
} catch (TimeoutException e) {
System.out.printf("Test timeout %d %s expired!\n", timeout, units.name());
dumpThreads();
StackTraceElement[] stackTrace = t.getStackTrace();
Exception currThreadException = new TestTimedOutException(timeout, units);
if (stackTrace != null) {
currThreadException.setStackTrace(stackTrace);
t.interrupt();
}
throw currThreadException;
}
}
Throwable throwable = error.get();
if (throwable != null) {
throw throwable;
}
}
};
}
use of java.util.concurrent.FutureTask in project jdk8u_jdk by JetBrains.
the class CThreading method executeOnAppKit.
public static <V> V executeOnAppKit(Callable<V> command) throws Throwable {
if (!isAppKit()) {
Dispatch dispatch = Dispatch.getInstance();
if (dispatch == null) {
throw new AWTError("Could not get Dispatch object");
}
FutureTask<V> future = new FutureTask<>(command);
dispatch.getNonBlockingMainQueueExecutor().execute(future);
try {
return future.get();
} catch (InterruptedException e) {
throw new AWTError(e.getMessage());
} catch (ExecutionException e) {
throw e.getCause();
}
} else
return command.call();
}
use of java.util.concurrent.FutureTask in project indy by Commonjava.
the class ThreadDumper method timeoutRule.
public static TestRule timeoutRule(int timeout, TimeUnit units) {
return (base, description) -> new Statement() {
public void evaluate() throws Throwable {
System.out.printf("Setting up timeout: %d %s to wrap: %s\n", timeout, units, base);
AtomicReference<Throwable> error = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
FutureTask<Void> task = new FutureTask<>(() -> {
try {
latch.countDown();
base.evaluate();
} catch (Throwable t) {
error.set(t);
}
return null;
});
ThreadGroup tg = new ThreadGroup("Test Timeout Group");
Thread t = new Thread(tg, task, "Test Timeout Thread");
t.setDaemon(true);
t.start();
try {
System.out.println("Waiting for test to start.");
latch.await();
} catch (InterruptedException e) {
error.set(e);
}
if (error.get() == null) {
try {
System.out.println("Waiting for test to complete (or timeout)");
task.get(timeout, units);
} catch (InterruptedException e) {
error.set(e);
} catch (ExecutionException e) {
error.set(e.getCause());
} catch (TimeoutException e) {
System.out.printf("Test timeout %d %s expired!\n", timeout, units.name());
dumpThreads();
StackTraceElement[] stackTrace = t.getStackTrace();
Exception currThreadException = new TestTimedOutException(timeout, units);
if (stackTrace != null) {
currThreadException.setStackTrace(stackTrace);
t.interrupt();
}
throw currThreadException;
}
}
Throwable throwable = error.get();
if (throwable != null) {
throw throwable;
}
}
};
}
use of java.util.concurrent.FutureTask in project android_frameworks_base by crdroidandroid.
the class MffTestCase method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
// MffContext needs to be created on a separate thread to allow MFF to post Runnable's.
mMffContextHandlerThread = new HandlerThread("MffContextThread");
mMffContextHandlerThread.start();
Handler handler = new Handler(mMffContextHandlerThread.getLooper());
FutureTask<MffContext> task = new FutureTask<MffContext>(new Callable<MffContext>() {
@Override
public MffContext call() throws Exception {
MffContext.Config config = new MffContext.Config();
config.requireCamera = false;
config.requireOpenGL = false;
config.forceNoGL = true;
return new MffContext(getContext(), config);
}
});
handler.post(task);
// Wait for the context to be created on the handler thread.
mMffContext = task.get();
}
use of java.util.concurrent.FutureTask in project double-espresso by JakeWharton.
the class AsyncTaskPoolMonitorTest method testIdleNotificationAndRestart.
public void testIdleNotificationAndRestart() throws Exception {
FutureTask<Thread> workerThreadFetchTask = new FutureTask<Thread>(new Callable<Thread>() {
@Override
public Thread call() {
return Thread.currentThread();
}
});
testThreadPool.submit(workerThreadFetchTask);
Thread workerThread = workerThreadFetchTask.get();
final CountDownLatch runLatch = new CountDownLatch(1);
final CountDownLatch exitLatch = new CountDownLatch(1);
testThreadPool.submit(new Runnable() {
@Override
public void run() {
runLatch.countDown();
try {
exitLatch.await();
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
}
}
});
assertTrue(runLatch.await(1, TimeUnit.SECONDS));
final CountDownLatch notificationLatch = new CountDownLatch(1);
monitor.notifyWhenIdle(new Runnable() {
@Override
public void run() {
notificationLatch.countDown();
}
});
// give some time for the idle detection threads to spin up.
Thread.sleep(2000);
// interrupt one of them
workerThread.interrupt();
Thread.sleep(1000);
// unblock the dummy work item.
exitLatch.countDown();
assertTrue(notificationLatch.await(1, TimeUnit.SECONDS));
assertTrue(monitor.isIdleNow());
}
Aggregations