use of java.lang.Thread.UncaughtExceptionHandler in project enroscar by stanfy.
the class AbstractMockServerTest method waitAndAssert.
public <T> void waitAndAssert(final Waiter<T> waiter, final Asserter<T> asserter) throws Throwable {
final Thread checker = new Thread() {
@Override
public void run() {
final T data = waiter.waitForData();
if (asserter != null) {
try {
asserter.makeAssertions(data);
} catch (final Exception e) {
error = e;
}
}
}
};
checker.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread thread, final Throwable ex) {
ex.printStackTrace();
error = ex;
throw new AssertionError("Exception occured: " + ex.getMessage());
}
});
checker.start();
final boolean[] res = new boolean[1];
try {
checker.join();
res[0] = true;
// watchdog.interrupt();
} catch (final InterruptedException e) {
throw new RuntimeException(e);
}
if (error != null) {
throw error;
}
if (!res[0]) {
throw new AssertionError("Too long");
}
}
use of java.lang.Thread.UncaughtExceptionHandler in project caffeine by ben-manes.
the class LocalLoadingCacheTest method disabled_testRecursiveComputation.
// Bug in JDK8; fixed but not released as of 1.8.0_25-b17
public void disabled_testRecursiveComputation() throws InterruptedException {
final AtomicReference<LoadingCache<Integer, String>> cacheRef = new AtomicReference<LoadingCache<Integer, String>>();
CacheLoader<Integer, String> recursiveLoader = new CacheLoader<Integer, String>() {
@Override
public String load(Integer key) {
if (key > 0) {
return key + ", " + cacheRef.get().getUnchecked(key - 1);
} else {
return "0";
}
}
};
LoadingCache<Integer, String> recursiveCache = CaffeinatedGuava.build(Caffeine.newBuilder().weakKeys().weakValues(), recursiveLoader);
cacheRef.set(recursiveCache);
assertEquals("3, 2, 1, 0", recursiveCache.getUnchecked(3));
recursiveLoader = new CacheLoader<Integer, String>() {
@Override
public String load(Integer key) {
return cacheRef.get().getUnchecked(key);
}
};
recursiveCache = CaffeinatedGuava.build(Caffeine.newBuilder().weakKeys().weakValues(), recursiveLoader);
cacheRef.set(recursiveCache);
// tells the test when the compution has completed
final CountDownLatch doneSignal = new CountDownLatch(1);
Thread thread = new Thread() {
@Override
public void run() {
try {
cacheRef.get().getUnchecked(3);
} finally {
doneSignal.countDown();
}
}
};
thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
}
});
thread.start();
boolean done = doneSignal.await(1, TimeUnit.SECONDS);
if (!done) {
StringBuilder builder = new StringBuilder();
for (StackTraceElement trace : thread.getStackTrace()) {
builder.append("\tat ").append(trace).append('\n');
}
fail(builder.toString());
}
}
use of java.lang.Thread.UncaughtExceptionHandler in project guava by google.
the class LocalLoadingCacheTest method testRecursiveComputation.
public void testRecursiveComputation() throws InterruptedException {
final AtomicReference<LoadingCache<Integer, String>> cacheRef = new AtomicReference<LoadingCache<Integer, String>>();
CacheLoader<Integer, String> recursiveLoader = new CacheLoader<Integer, String>() {
@Override
public String load(Integer key) {
if (key > 0) {
return key + ", " + cacheRef.get().getUnchecked(key - 1);
} else {
return "0";
}
}
};
LoadingCache<Integer, String> recursiveCache = new CacheBuilder<Integer, String>().weakKeys().weakValues().build(recursiveLoader);
cacheRef.set(recursiveCache);
assertEquals("3, 2, 1, 0", recursiveCache.getUnchecked(3));
recursiveLoader = new CacheLoader<Integer, String>() {
@Override
public String load(Integer key) {
return cacheRef.get().getUnchecked(key);
}
};
recursiveCache = new CacheBuilder<Integer, String>().weakKeys().weakValues().build(recursiveLoader);
cacheRef.set(recursiveCache);
// tells the test when the compution has completed
final CountDownLatch doneSignal = new CountDownLatch(1);
Thread thread = new Thread() {
@Override
public void run() {
try {
cacheRef.get().getUnchecked(3);
} finally {
doneSignal.countDown();
}
}
};
thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
}
});
thread.start();
boolean done = doneSignal.await(1, TimeUnit.SECONDS);
if (!done) {
StringBuilder builder = new StringBuilder();
for (StackTraceElement trace : thread.getStackTrace()) {
builder.append("\tat ").append(trace).append('\n');
}
fail(builder.toString());
}
}
use of java.lang.Thread.UncaughtExceptionHandler in project j2objc by google.
the class ThreadTest method test_get_setUncaughtExceptionHandler.
/**
* @tests java.lang.Thread#getUncaughtExceptionHandler
* @tests java.lang.Thread#setUncaughtExceptionHandler
*/
public void test_get_setUncaughtExceptionHandler() {
class Handler implements UncaughtExceptionHandler {
public void uncaughtException(Thread thread, Throwable ex) {
}
}
final Handler handler = new Handler();
Thread.currentThread().setUncaughtExceptionHandler(handler);
assertSame(handler, Thread.currentThread().getUncaughtExceptionHandler());
Thread.currentThread().setUncaughtExceptionHandler(null);
}
use of java.lang.Thread.UncaughtExceptionHandler in project j2objc by google.
the class ForkJoinPool method makeCommonPool.
/**
* Creates and returns the common pool, respecting user settings
* specified via system properties.
*/
private static ForkJoinPool makeCommonPool() {
int parallelism = -1;
ForkJoinWorkerThreadFactory factory = defaultForkJoinWorkerThreadFactory;
UncaughtExceptionHandler handler = null;
try {
// ignore exceptions in accessing/parsing properties
String pp = System.getProperty("java.util.concurrent.ForkJoinPool.common.parallelism");
String fp = System.getProperty("java.util.concurrent.ForkJoinPool.common.threadFactory");
String hp = System.getProperty("java.util.concurrent.ForkJoinPool.common.exceptionHandler");
if (pp != null)
parallelism = Integer.parseInt(pp);
if (fp != null)
factory = ((ForkJoinWorkerThreadFactory) ClassLoader.getSystemClassLoader().loadClass(fp).newInstance());
if (hp != null)
handler = ((UncaughtExceptionHandler) ClassLoader.getSystemClassLoader().loadClass(hp).newInstance());
} catch (Exception ignore) {
}
if (// default 1 less than #cores
parallelism < 0 && (parallelism = Runtime.getRuntime().availableProcessors() - 1) < 0)
parallelism = 0;
if (parallelism > MAX_CAP)
parallelism = MAX_CAP;
return new ForkJoinPool(parallelism, factory, handler, LIFO_QUEUE, "ForkJoinPool.commonPool-worker-");
}
Aggregations