Search in sources :

Example 21 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project guava by hceylan.

the class ThreadFactoryBuilder method build.

private static ThreadFactory build(ThreadFactoryBuilder builder) {
    final String nameFormat = builder.nameFormat;
    final Boolean daemon = builder.daemon;
    final Integer priority = builder.priority;
    final UncaughtExceptionHandler uncaughtExceptionHandler = builder.uncaughtExceptionHandler;
    final ThreadFactory backingThreadFactory = (builder.backingThreadFactory != null) ? builder.backingThreadFactory : Executors.defaultThreadFactory();
    final AtomicLong count = (nameFormat != null) ? new AtomicLong(0) : null;
    return new ThreadFactory() {

        @Override
        public Thread newThread(Runnable runnable) {
            Thread thread = backingThreadFactory.newThread(runnable);
            if (nameFormat != null) {
                thread.setName(String.format(nameFormat, count.getAndIncrement()));
            }
            if (daemon != null) {
                thread.setDaemon(daemon);
            }
            if (priority != null) {
                thread.setPriority(priority);
            }
            if (uncaughtExceptionHandler != null) {
                thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
            }
            return thread;
        }
    };
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 22 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project guava by hceylan.

the class AbstractServiceTest method invokeOnExecutionThreadForTest.

private void invokeOnExecutionThreadForTest(Runnable runnable) {
    executionThread = new Thread(runnable);
    executionThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable e) {
            thrownByExecutionThread = e;
        }
    });
    executionThread.start();
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) Thread.currentThread(java.lang.Thread.currentThread)

Example 23 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project android_frameworks_base by AOSPA.

the class UiAutomatorTestRunner method run.

public void run(List<String> testClasses, Bundle params, boolean debug, boolean monkey) {
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            Log.e(LOGTAG, "uncaught exception", ex);
            Bundle results = new Bundle();
            results.putString("shortMsg", ex.getClass().getName());
            results.putString("longMsg", ex.getMessage());
            mWatcher.instrumentationFinished(null, 0, results);
            // bailing on uncaught exception
            System.exit(EXIT_EXCEPTION);
        }
    });
    mTestClasses = testClasses;
    mParams = params;
    mDebug = debug;
    mMonkey = monkey;
    start();
    System.exit(EXIT_OK);
}
Also used : Bundle(android.os.Bundle) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) HandlerThread(android.os.HandlerThread)

Example 24 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project opennms by OpenNMS.

the class EventIpcManagerDefaultImplTest method setUp.

@Override
public void setUp() throws Exception {
    m_manager = new EventIpcManagerDefaultImpl(m_registry);
    m_manager.setEventHandler(m_eventHandler);
    m_manager.setHandlerPoolSize(5);
    m_manager.afterPropertiesSet();
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            m_caughtThrowable = throwable;
            m_caughtThrowableThread = thread;
        }
    });
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 25 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project voltdb by VoltDB.

the class ThreadLocalRandom method getInitialSeedUniquifier.

public static synchronized long getInitialSeedUniquifier() {
    // Use the value set via the setter.
    long initialSeedUniquifier = ThreadLocalRandom.initialSeedUniquifier;
    if (initialSeedUniquifier == 0) {
        // Use the system property value.
        ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier = AccessController.doPrivileged(new PrivilegedAction<Long>() {

            @Override
            public Long run() {
                return Long.getLong("io.netty.initialSeedUniquifier", 0);
            }
        });
    }
    // Otherwise, generate one.
    if (initialSeedUniquifier == 0) {
        boolean secureRandom = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {

            @Override
            public Boolean run() {
                return Boolean.getBoolean("java.util.secureRandomSeed");
            }
        });
        if (secureRandom) {
            // Try to generate a real random number from /dev/random.
            // Get from a different thread to avoid blocking indefinitely on a machine without much entropy.
            final BlockingQueue<Long> queue = new LinkedBlockingQueue<Long>();
            Thread generatorThread = new Thread("initialSeedUniquifierGenerator") {

                @Override
                public void run() {
                    // Get the real random seed from /dev/random
                    SecureRandom random = new SecureRandom();
                    final byte[] seed = random.generateSeed(8);
                    long s = ((long) seed[0] & 0xff) << 56 | ((long) seed[1] & 0xff) << 48 | ((long) seed[2] & 0xff) << 40 | ((long) seed[3] & 0xff) << 32 | ((long) seed[4] & 0xff) << 24 | ((long) seed[5] & 0xff) << 16 | ((long) seed[6] & 0xff) << 8 | (long) seed[7] & 0xff;
                    queue.add(s);
                }
            };
            generatorThread.setDaemon(true);
            generatorThread.start();
            generatorThread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

                @Override
                public void uncaughtException(Thread t, Throwable e) {
                    logger.debug("An exception has been raised by {}", t.getName(), e);
                }
            });
            // Get the random seed from the thread with timeout.
            final long timeoutSeconds = 3;
            final long deadLine = System.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds);
            boolean interrupted = false;
            for (; ; ) {
                long waitTime = deadLine - System.nanoTime();
                if (waitTime <= 0) {
                    generatorThread.interrupt();
                    logger.warn("Failed to generate a seed from SecureRandom within {} seconds. " + "Not enough entrophy?", timeoutSeconds);
                    break;
                }
                try {
                    Long seed = queue.poll(waitTime, TimeUnit.NANOSECONDS);
                    if (seed != null) {
                        initialSeedUniquifier = seed;
                        break;
                    }
                } catch (InterruptedException e) {
                    interrupted = true;
                    logger.warn("Failed to generate a seed from SecureRandom due to an InterruptedException.");
                    break;
                }
            }
            // Just in case the initialSeedUniquifier is zero or some other constant
            // just a meaningless random number
            initialSeedUniquifier ^= 0x3255ecdc33bae119L;
            initialSeedUniquifier ^= Long.reverse(System.nanoTime());
            if (interrupted) {
                // Restore the interrupt status because we don't know how to/don't need to handle it here.
                Thread.currentThread().interrupt();
                // Interrupt the generator thread if it's still running,
                // in the hope that the SecureRandom provider raises an exception on interruption.
                generatorThread.interrupt();
            }
        } else {
            initialSeedUniquifier = mix64(System.currentTimeMillis()) ^ mix64(System.nanoTime());
        }
        ThreadLocalRandom.initialSeedUniquifier = initialSeedUniquifier;
    }
    return initialSeedUniquifier;
}
Also used : SecureRandom(java.security.SecureRandom) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AtomicLong(java.util.concurrent.atomic.AtomicLong) 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