Search in sources :

Example 6 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project RxJava by ReactiveX.

the class RxJavaPluginsTest method onErrorCrashes.

@Test
public void onErrorCrashes() {
    try {
        final List<Throwable> list = new ArrayList<Throwable>();
        RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

            @Override
            public void accept(Throwable t) {
                throw new TestException("Forced failure 2");
            }
        });
        Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                list.add(e);
            }
        });
        RxJavaPlugins.onError(new TestException("Forced failure"));
        assertEquals(2, list.size());
        assertTestException(list, 0, "Forced failure 2");
        assertUndeliverableTestException(list, 1, "Forced failure");
        Thread.currentThread().setUncaughtExceptionHandler(null);
    } finally {
        RxJavaPlugins.reset();
        Thread.currentThread().setUncaughtExceptionHandler(null);
    }
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 7 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project RxJava by ReactiveX.

the class RxJavaPluginsTest method onErrorWithNull.

@Test
public void onErrorWithNull() {
    try {
        final List<Throwable> list = new ArrayList<Throwable>();
        RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {

            @Override
            public void accept(Throwable t) {
                throw new TestException("Forced failure 2");
            }
        });
        Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                list.add(e);
            }
        });
        RxJavaPlugins.onError(null);
        assertEquals(2, list.size());
        assertTestException(list, 0, "Forced failure 2");
        assertNPE(list, 1);
        RxJavaPlugins.reset();
        RxJavaPlugins.onError(null);
        assertNPE(list, 2);
    } finally {
        RxJavaPlugins.reset();
        Thread.currentThread().setUncaughtExceptionHandler(null);
    }
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 8 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project OpenGrok by OpenGrok.

the class Executor method registerErrorHandler.

public static void registerErrorHandler() {
    UncaughtExceptionHandler dueh = Thread.currentThread().getDefaultUncaughtExceptionHandler();
    if (dueh == null) {
        LOGGER.log(Level.FINE, "Installing default uncaught exception handler");
        Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                LOGGER.log(Level.SEVERE, "Uncaught exception in thread " + t.getName() + " with ID " + t.getId() + ": " + e.getMessage(), e);
            }
        });
    }
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 9 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project jersey by jersey.

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 10 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project randomizedtesting by randomizedtesting.

the class LocalSlaveStreamHandler method start.

@Override
public void start() throws IOException {
    lastActivity = System.currentTimeMillis();
    pumpers.add(new Thread(new StreamPumper(stdout, sysout), "pumper-stdout"));
    pumpers.add(new Thread(new StreamPumper(stderr, syserr), "pumper-stderr"));
    pumpers.add(new Thread("pumper-events") {

        public void run() {
            pumpEvents(eventStream);
        }
    });
    if (heartbeat > 0) {
        pumpers.add(watchdog = new Thread("pumper-watchdog") {

            public void run() {
                final long heartbeatMillis = TimeUnit.SECONDS.toMillis(heartbeat);
                final long HEARTBEAT = Math.max(500, heartbeatMillis / 5);
                final long HEARTBEAT_EVENT_THRESHOLD = heartbeatMillis;
                try {
                    long lastObservedUpdate = lastActivity;
                    long reportDeadline = lastObservedUpdate + HEARTBEAT_EVENT_THRESHOLD;
                    while (true) {
                        Thread.sleep(HEARTBEAT);
                        Long last = lastActivity;
                        if (last == null) {
                            // terminated.
                            break;
                        }
                        if (last != lastObservedUpdate) {
                            lastObservedUpdate = last;
                            reportDeadline = last + HEARTBEAT_EVENT_THRESHOLD;
                        } else {
                            final long current = System.currentTimeMillis();
                            if (current >= reportDeadline) {
                                eventBus.post(new LowLevelHeartBeatEvent(last, current));
                                reportDeadline = System.currentTimeMillis() + HEARTBEAT_EVENT_THRESHOLD;
                            }
                        }
                    }
                } catch (InterruptedException e) {
                // terminate on interrupt.
                }
            }
        });
    }
    // Start all pumper threads.
    UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {

        public void uncaughtException(Thread t, Throwable e) {
            warnStream.println("Unhandled exception in thread: " + t);
            e.printStackTrace(warnStream);
        }
    };
    for (Thread t : pumpers) {
        t.setUncaughtExceptionHandler(handler);
        t.setDaemon(true);
        t.start();
    }
}
Also used : LowLevelHeartBeatEvent(com.carrotsearch.ant.tasks.junit4.events.LowLevelHeartBeatEvent) StreamPumper(org.apache.tools.ant.taskdefs.StreamPumper) 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