Search in sources :

Example 26 with UncaughtExceptionHandler

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

the class ForkJoinPool method registerWorker.

//  Registering and deregistering workers
/**
     * Callback from ForkJoinWorkerThread to establish and record its
     * WorkQueue. To avoid scanning bias due to packing entries in
     * front of the workQueues array, we treat the array as a simple
     * power-of-two hash table using per-thread seed as hash,
     * expanding as needed.
     *
     * @param wt the worker thread
     * @return the worker's queue
     */
final WorkQueue registerWorker(ForkJoinWorkerThread wt) {
    UncaughtExceptionHandler handler;
    WorkQueue[] ws;
    int s, ps;
    wt.setDaemon(true);
    if ((handler = ueh) != null)
        wt.setUncaughtExceptionHandler(handler);
    do {
    } while (!U.compareAndSwapInt(this, INDEXSEED, s = indexSeed, s += SEED_INCREMENT) || // skip 0
    s == 0);
    WorkQueue w = new WorkQueue(this, wt, mode, s);
    if (((ps = plock) & PL_LOCK) != 0 || !U.compareAndSwapInt(this, PLOCK, ps, ps += PL_LOCK))
        ps = acquirePlock();
    int nps = (ps & SHUTDOWN) | ((ps + PL_LOCK) & ~SHUTDOWN);
    try {
        if ((ws = workQueues) != null) {
            // skip if shutting down
            int n = ws.length, m = n - 1;
            // use odd-numbered indices
            int r = (s << 1) | 1;
            if (ws[r &= m] != null) {
                // collision
                // step by approx half size
                int probes = 0;
                int step = (n <= 4) ? 2 : ((n >>> 1) & EVENMASK) + 2;
                while (ws[r = (r + step) & m] != null) {
                    if (++probes >= n) {
                        workQueues = ws = Arrays.copyOf(ws, n <<= 1);
                        m = n - 1;
                        probes = 0;
                    }
                }
            }
            w.poolIndex = (short) r;
            // volatile write orders
            w.eventCount = r;
            ws[r] = w;
        }
    } finally {
        if (!U.compareAndSwapInt(this, PLOCK, ps, nps))
            releasePlock(nps);
    }
    wt.setName(workerNamePrefix.concat(Integer.toString(w.poolIndex >>> 1)));
    return w;
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 27 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project jackrabbit-oak by apache.

the class ConcurrentAddNodesClusterIT method addNodesConcurrent2.

@Ignore("OAK-1807")
@Test
public void addNodesConcurrent2() throws Exception {
    final Thread mainThread = Thread.currentThread();
    for (int i = 0; i < NUM_CLUSTER_NODES; i++) {
        DocumentMK mk = new DocumentMK.Builder().setMongoDB(createConnection().getDB()).setClusterId(i + 1).open();
        mks.add(mk);
    }
    final Map<String, Exception> exceptions = Collections.synchronizedMap(new HashMap<String, Exception>());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean stop = new AtomicBoolean();
    final UncaughtExceptionHandler ueh = new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            RuntimeException r = new RuntimeException("Exception in thread " + t.getName(), e);
            r.printStackTrace();
        }
    };
    for (int i = 0; i < mks.size(); i++) {
        DocumentMK mk = mks.get(i);
        final Repository repo = new Jcr(mk.getNodeStore()).createRepository();
        repos.add(repo);
        for (int w = 0; w <= WORKER_COUNT; w++) {
            final String name = "Worker-" + (i + 1) + "-" + (w + 1);
            final Runnable r = new Runnable() {

                final Session session = createAdminSession(repo);

                int count = 0;

                @Override
                public void run() {
                    try {
                        Uninterruptibles.awaitUninterruptibly(latch);
                        session.refresh(false);
                        Node node = session.getRootNode().addNode(name + count++, "oak:Unstructured");
                        for (int j = 0; j < NODE_COUNT && !stop.get(); j++) {
                            node.addNode("node" + j);
                            session.save();
                        }
                    } catch (RepositoryException e) {
                        RuntimeException r = new RuntimeException("Exception in thread " + name, e);
                        r.printStackTrace();
                        exceptions.put(Thread.currentThread().getName(), r);
                        stop.set(true);
                        mainThread.interrupt();
                    } finally {
                        session.logout();
                    }
                }
            };
            //Last runnable would be a long running one
            Runnable runnable = r;
            if (w == WORKER_COUNT) {
                runnable = new Runnable() {

                    @Override
                    public void run() {
                        while (!stop.get()) {
                            r.run();
                        }
                    }
                };
            }
            Thread t = new Thread(runnable);
            t.setName(name);
            t.setUncaughtExceptionHandler(ueh);
            workers.add(t);
        }
    }
    for (Thread t : workers) {
        t.start();
    }
    latch.countDown();
    TimeUnit.MINUTES.sleep(10);
    stop.set(true);
    for (Thread t : workers) {
        t.join();
    }
    for (Map.Entry<String, Exception> entry : exceptions.entrySet()) {
        // System.out.println("exception in thread " + entry.getKey());
        throw entry.getValue();
    }
}
Also used : DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) CountDownLatch(java.util.concurrent.CountDownLatch) RepositoryException(javax.jcr.RepositoryException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Repository(javax.jcr.Repository) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) HashMap(java.util.HashMap) Map(java.util.Map) Session(javax.jcr.Session) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 28 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project indy by Commonjava.

the class JaxRsBooter method main.

public static void main(final String[] args) {
    Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(final Thread thread, final Throwable error) {
            if (error instanceof InvocationTargetException) {
                final InvocationTargetException ite = (InvocationTargetException) error;
                System.err.println("In: " + thread.getName() + "(" + thread.getId() + "), caught InvocationTargetException:");
                ite.getTargetException().printStackTrace();
                System.err.println("...via:");
                error.printStackTrace();
            } else {
                System.err.println("In: " + thread.getName() + "(" + thread.getId() + ") Uncaught error:");
                error.printStackTrace();
            }
        }
    });
    BootOptions boot;
    try {
        boot = BootOptions.loadFromSysprops();
    } catch (final IndyBootException e) {
        System.err.printf("ERROR: %s", e.getMessage());
        System.exit(ERR_CANT_LOAD_BOOT_OPTIONS);
        return;
    }
    try {
        if (boot.parseArgs(args)) {
            try {
                final BootInterface booter = new JaxRsBooter();
                System.out.println("Starting Indy booter: " + booter);
                final int result = booter.runAndWait(boot);
                if (result != 0) {
                    System.exit(result);
                }
            } catch (final IndyBootException e) {
                System.err.printf("ERROR INITIALIZING BOOTER: %s", e.getMessage());
                System.exit(ERR_CANT_INIT_BOOTER);
            }
        }
    } catch (final IndyBootException e) {
        System.err.printf("ERROR: %s", e.getMessage());
        System.exit(ERR_CANT_PARSE_ARGS);
    }
}
Also used : WeldBootInterface(org.commonjava.indy.boot.WeldBootInterface) BootInterface(org.commonjava.indy.boot.BootInterface) IndyBootException(org.commonjava.indy.boot.IndyBootException) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) BootOptions(org.commonjava.indy.boot.BootOptions) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 29 with UncaughtExceptionHandler

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

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

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

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(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)

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