Search in sources :

Example 16 with UncaughtExceptionHandler

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");
    }
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) IOException(java.io.IOException)

Example 17 with UncaughtExceptionHandler

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());
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 18 with UncaughtExceptionHandler

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());
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) LocalLoadingCache(com.google.common.cache.LocalCache.LocalLoadingCache) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 19 with UncaughtExceptionHandler

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);
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 20 with UncaughtExceptionHandler

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-");
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Aggregations

UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)51 Bundle (android.os.Bundle)5 HandlerThread (android.os.HandlerThread)5 IOException (java.io.IOException)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ThreadFactory (java.util.concurrent.ThreadFactory)4 Test (org.junit.Test)4 LocalLoadingCache (com.google.common.cache.LocalCache.LocalLoadingCache)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 Intent (android.content.Intent)1 Message (android.os.Message)1 PowerManager (android.os.PowerManager)1 RemoteException (android.os.RemoteException)1 ConsoleMessage (android.webkit.ConsoleMessage)1 SmartThread (com.alibaba.jstorm.utils.SmartThread)1 JCommander (com.beust.jcommander.JCommander)1 Console (com.beust.jcommander.internal.Console)1 LowLevelHeartBeatEvent (com.carrotsearch.ant.tasks.junit4.events.LowLevelHeartBeatEvent)1