Example usage for java.lang Thread getName

List of usage examples for java.lang Thread getName

Introduction

In this page you can find the example usage for java.lang Thread getName.

Prototype

public final String getName() 

Source Link

Document

Returns this thread's name.

Usage

From source file:com.kotcrab.vis.editor.util.CrashReporter.java

private void printThreadInfo() {
    println("--- Threads ---");

    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

    for (Thread t : threadSet) {
        if (t.isDaemon()) {
            println("Skipping daemon thread: " + t.getName());
        } else {/*from   w w  w  .  j a  v a 2 s. c  o  m*/
            println("Thread: " + t.getName());
            for (StackTraceElement e : t.getStackTrace()) {
                crashReport.append("\t");
                println(e.toString());
            }
        }

        println();
    }

    println("---------------");
    println();
}

From source file:org.apache.hadoop.hdfs.TestHttpServerShutdown.java

@Test(timeout = 180000)
public void testShutdown() throws Exception {
    oldThreads = Thread.getAllStackTraces().keySet();
    conf = new Configuration();
    conf.setInt(HttpServer.HTTP_THREADPOOL_MAX_STOP_TIME, 3000);
    cluster = new MiniDFSCluster(conf, 0, true, null);

    InjectionHandler.set(new TestHandler());
    Thread fsck = new FsckThread();
    fsck.setDaemon(true);/*from   ww  w  .  j av  a  2  s.  c om*/
    fsck.start();

    while (!fsckCalled) {
        Thread.sleep(1000);
        LOG.info("Waiting for fsck to hit NN");
    }
    cluster.shutdown();

    LOG.info("Alive Non Daemon threads : ");
    for (Map.Entry<Thread, StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet()) {
        Thread t = entry.getKey();
        if (!t.isDaemon() && !oldThreads.contains(t)) {
            LOG.info("Thread : " + t.getName());
            for (StackTraceElement e : entry.getValue()) {
                LOG.info(e);
            }
            fail("Thread : " + t.getName() + " is not a daemon thread");
        }
    }
}

From source file:controller.servlet.AllDataDelete.java

/**
 * Mtodo stopThreads, permite parar los hilos en ejecucin.
 *//*from  www  .j  a v  a2s .  c o m*/
private void stopThreads() {
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
    for (Thread thread : threadArray) {
        if (thread.getName().equals(ThreadName.TRACK_THREAD_NAME)
                || thread.getName().equals(ThreadName.AMENITIES_THREAD_NAME)) {
            thread.interrupt();
            thread.stop();
        }
    }
}

From source file:net.sourceforge.vulcan.scheduler.thread.AbstractScheduler.java

public synchronized void uncaughtException(Thread thread, Throwable e) {
    log.error("Uncaught throwable in thread " + thread.getName(), e);
    if (this.thread == thread) {
        this.thread = null;
        this.running = false;
        this.shutdown = true;
    }//from  w  w w. jav  a 2s  . c  o  m
}

From source file:SwingWorker.java

/**
 * returns workersExecutorService./*from   www . j  a v  a  2 s.  c om*/
 *
 * returns the service stored in the appContext or creates it if
 * necessary. If the last one it triggers autoShutdown thread to
 * get started.
 * 
 * @return ExecutorService for the {@code SwingWorkers}
 * @see #startAutoShutdownThread
 */
private static synchronized ExecutorService getWorkersExecutorService() {
    if (executorService == null) {
        //this creates non-daemon threads. 
        ThreadFactory threadFactory = new ThreadFactory() {
            final ThreadFactory defaultFactory = Executors.defaultThreadFactory();

            public Thread newThread(final Runnable r) {
                Thread thread = defaultFactory.newThread(r);
                thread.setName("SwingWorker-" + thread.getName());
                return thread;
            }
        };

        /*
         * We want a to have no more than MAX_WORKER_THREADS
         * running threads.
         *
         * We want a worker thread to wait no longer than 1 second
         * for new tasks before terminating.
         */
        executorService = new ThreadPoolExecutor(0, MAX_WORKER_THREADS, 1L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(), threadFactory) {

            private final ReentrantLock pauseLock = new ReentrantLock();
            private final Condition unpaused = pauseLock.newCondition();
            private boolean isPaused = false;
            private final ReentrantLock executeLock = new ReentrantLock();

            @Override
            public void execute(Runnable command) {
                /*
                 * ThreadPoolExecutor first tries to run task
                 * in a corePool. If all threads are busy it
                 * tries to add task to the waiting queue. If it
                 * fails it run task in maximumPool.
                 *
                 * We want corePool to be 0 and
                 * maximumPool to be MAX_WORKER_THREADS
                 * We need to change the order of the execution.
                 * First try corePool then try maximumPool
                 * pool and only then store to the waiting
                 * queue. We can not do that because we would
                 * need access to the private methods.
                 *
                 * Instead we enlarge corePool to
                 * MAX_WORKER_THREADS before the execution and
                 * shrink it back to 0 after. 
                 * It does pretty much what we need.
                 *
                 * While we changing the corePoolSize we need
                 * to stop running worker threads from accepting new
                 * tasks.
                 */

                //we need atomicity for the execute method.
                executeLock.lock();
                try {

                    pauseLock.lock();
                    try {
                        isPaused = true;
                    } finally {
                        pauseLock.unlock();
                    }

                    setCorePoolSize(MAX_WORKER_THREADS);
                    super.execute(command);
                    setCorePoolSize(0);

                    pauseLock.lock();
                    try {
                        isPaused = false;
                        unpaused.signalAll();
                    } finally {
                        pauseLock.unlock();
                    }
                } finally {
                    executeLock.unlock();
                }
            }

            @Override
            protected void afterExecute(Runnable r, Throwable t) {
                super.afterExecute(r, t);
                pauseLock.lock();
                try {
                    while (isPaused) {
                        unpaused.await();
                    }
                } catch (InterruptedException ignore) {

                } finally {
                    pauseLock.unlock();
                }
            }
        };
    }
    return executorService;
}

From source file:org.grouplens.lenskit.util.parallel.TaskGraphManager.java

public synchronized void threadTerminating() {
    Thread thread = Thread.currentThread();
    logger.debug("thread {} finished", thread.getName());
    threads.remove(thread);// w  ww  .  j a  va 2s . c om
    notifyAll();
}

From source file:de.thischwa.pmcms.view.renderer.ExportThreadPoolController.java

synchronized void interrupt(final Thread thread, final Exception e) {
    threadException = e;// w w w.j  av a 2  s.co  m
    isError = true;
    shutdownNow();
    logger.error(thread.getName().concat(" causes an error: ").concat(e.getMessage()), e);
}

From source file:org.thingsplode.agent.monitors.MonitoringExecutor.java

@PostConstruct
public void init() {
    scheduler = Executors.newScheduledThreadPool(schedulerThreadPoolSize, (Runnable r) -> {
        Thread t = new Thread(r, "SCHEDULER");
        t.setDaemon(true);//  w  ww  .j a v a  2  s  .  c  o m
        t.setUncaughtExceptionHandler((Thread t1, Throwable e) -> {
            logger.error(String.format("Uncaught exception on thread %s. Exception %s with message %s.",
                    t1.getName(), e.getClass().getSimpleName(), e.getMessage()), e);
        });
        return t;
    });
    if (autoInitializeSystemMetricProvider) {
        addProvider(new SystemMetricProvider(), 60);
    }
    if (autoInitializeThreadMetricProvider) {
        addProvider(new ThreadMetricProvider(), 300);
    }
    scheduleProviders();
}

From source file:ThreadTester.java

public void uncaughtException(Thread t, Throwable e) {
    System.err.printf("%s: %s at line %d of %s%n", t.getName(), e.toString(),
            e.getStackTrace()[0].getLineNumber(), e.getStackTrace()[0].getFileName());
}

From source file:org.bonitasoft.engine.LocalServerTestsInitializer.java

private boolean isCacheManager(Thread thread) {
    return thread.getName().startsWith("net.sf.ehcache.CacheManager");
}