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:org.tranche.logs.LogUtil.java

/**
 * //from   w w w. j a va  2 s  .  c o  m
 * @return
 */
public static final String getThreadDump() {
    Map<Thread, StackTraceElement[]> threadInfo = Thread.getAllStackTraces();
    StringBuffer buf = new StringBuffer();
    buf.append("Thread dump: " + threadInfo.size() + " threads");
    buf.append("\n" + "\n");
    for (Thread t : threadInfo.keySet()) {
        StackTraceElement[] ste = threadInfo.get(t);
        String daemonMsg = t.isDaemon() ? "daemon" : "non-daemon";
        String aliveMsg = t.isAlive() ? "alive" : "non-alive";
        buf.append("    * " + t.getName() + " (priority: " + t.getPriority() + ", " + daemonMsg + ", "
                + aliveMsg + ", state: " + t.getState() + ") ");
        buf.append("\n");

        for (int i = 0; i < ste.length; i++) {
            buf.append("        " + ste[i].toString());
            buf.append("\n");
        }

        buf.append("\n");
    }
    buf.append("\n" + "\n");
    return buf.toString();
}

From source file:org.kuali.ole.sys.context.SpringContext.java

static void initMonitoringThread() {
    ConfigurationService configurationService = GlobalResourceLoader.getService("kualiConfigurationService");
    if (configurationService.getPropertyValueAsBoolean("periodic.thread.dump")) {
        final long sleepPeriod = Long.parseLong(
                configurationService.getPropertyValueAsString("periodic.thread.dump.seconds")) * 1000;
        final File logDir = new File(configurationService.getPropertyValueAsString("logs.directory"));
        final File monitoringLogDir = new File(logDir, "monitoring");
        if (!monitoringLogDir.exists()) {
            monitoringLogDir.mkdir();//  ww  w .  j a  va  2 s .co  m
        }
        if (LOG.isInfoEnabled()) {
            LOG.info("Starting the Periodic Thread Dump thread - dumping every " + (sleepPeriod / 1000)
                    + " seconds");
            LOG.info("Periodic Thread Dump Logs: " + monitoringLogDir.getAbsolutePath());
        }
        final DateFormat df = new SimpleDateFormat("yyyyMMdd");
        final DateFormat tf = new SimpleDateFormat("HH-mm-ss");
        Runnable processWatch = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    Date now = new Date();
                    File todaysLogDir = new File(monitoringLogDir, df.format(now));
                    if (!todaysLogDir.exists()) {
                        todaysLogDir.mkdir();
                    }
                    File logFile = new File(todaysLogDir, "process-" + tf.format(now) + ".log");
                    try {
                        StringBuilder logStatement = new StringBuilder(10240);
                        logStatement.append("Threads Running at: ").append(now).append("\n\n\n");
                        Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
                        List<Thread> sortedThreads = new ArrayList<Thread>(threads.keySet());
                        Collections.sort(sortedThreads, new Comparator<Thread>() {
                            @Override
                            public int compare(Thread o1, Thread o2) {
                                return o1.getName().compareTo(o2.getName());
                            }
                        });
                        for (Thread t : sortedThreads) {
                            logStatement.append("\tThread: name=").append(t.getName()).append(", id=")
                                    .append(t.getId()).append(", priority=").append(t.getPriority())
                                    .append(", state=").append(t.getState());
                            logStatement.append('\n');
                            for (StackTraceElement stackTraceElement : threads.get(t)) {
                                logStatement.append("\t\t" + stackTraceElement).append('\n');
                            }
                            logStatement.append('\n');
                        }
                        FileUtils.writeStringToFile(logFile, logStatement.toString(), "UTF-8");
                    } catch (IOException ex) {
                        LOG.error("Unable to write the ProcessWatch output file: " + logFile.getAbsolutePath(),
                                ex);
                    }
                    try {
                        Thread.sleep(sleepPeriod);
                    } catch (InterruptedException ex) {
                        LOG.error("woken up during sleep of the ProcessWatch thread", ex);
                    }
                }
            }
        };
        processWatchThread = new Thread(processWatch, "ProcessWatch thread");
        processWatchThread.setDaemon(true);
        processWatchThread.start();
    }
}

From source file:org.nebulaframework.grid.Grid.java

private static void initializeDefaultExceptionHandler() {
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override/*  www  .j  a va  2  s . co m*/
        public void uncaughtException(Thread t, Throwable e) {
            log.fatal("[Uncaught Thread Exception] on Thread " + t.getName() + " - " + e, e);
            e.printStackTrace();
        }

    });
}

From source file:com.ery.estorm.util.Threads.java

/**
 * @param t/*from  w  w  w  . j av  a2 s  . c  o  m*/
 *            Waits on the passed thread to die dumping a threaddump every minute while its up.
 * @throws InterruptedException
 */
public static void threadDumpingIsAlive(final Thread t) throws InterruptedException {
    if (t == null) {
        return;
    }

    while (t.isAlive()) {
        t.join(60 * 1000);
        if (t.isAlive()) {
            ReflectionUtils.printThreadInfo(new PrintWriter(System.out),
                    "Automatic Stack Trace every 60 seconds waiting on " + t.getName());
        }
    }
}

From source file:com.nubits.nubot.utils.Utils.java

public static void logActiveThreads() {
    int active = Thread.activeCount();
    LOG.trace("currently active threads: " + active);
    Thread allThreads[] = new Thread[active];
    Thread.enumerate(allThreads);

    for (int i = 0; i < active; i++) {
        Thread t = allThreads[i];
        LOG.trace(i + ": " + t + " id: " + t.getId() + " name: " + t.getName() + " " + t.getContextClassLoader()
                + " group: " + t.getThreadGroup() + " alive" + t.isAlive());
        LOG.trace("super: " + t.getClass().getSuperclass());
    }/*from  w  w  w.ja  va2 s .  c om*/

    if (active > maxThreadsError) {
        LOG.error("too many threads started");
    }
}

From source file:org.kuali.kfs.sys.context.SpringContext.java

static void initMonitoringThread() {
    if (KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsBoolean("periodic.thread.dump")) {
        final long sleepPeriod = Long.parseLong(KRADServiceLocator.getKualiConfigurationService()
                .getPropertyValueAsString("periodic.thread.dump.seconds")) * 1000;
        final File logDir = new File(
                KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString("logs.directory"));
        final File monitoringLogDir = new File(logDir, "monitoring");
        if (!monitoringLogDir.exists()) {
            monitoringLogDir.mkdir();/*from   ww  w .j  a  v  a  2 s.  c  om*/
        }
        if (LOG.isInfoEnabled()) {
            LOG.info("Starting the Periodic Thread Dump thread - dumping every " + (sleepPeriod / 1000)
                    + " seconds");
            LOG.info("Periodic Thread Dump Logs: " + monitoringLogDir.getAbsolutePath());
        }
        final DateFormat df = new SimpleDateFormat("yyyyMMdd");
        final DateFormat tf = new SimpleDateFormat("HH-mm-ss");
        Runnable processWatch = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    Date now = new Date();
                    File todaysLogDir = new File(monitoringLogDir, df.format(now));
                    if (!todaysLogDir.exists()) {
                        todaysLogDir.mkdir();
                    }
                    File logFile = new File(todaysLogDir, "process-" + tf.format(now) + ".log");
                    try {
                        createParentDirs(logFile);
                        BufferedWriter w = new BufferedWriter(new FileWriter(logFile));
                        StringBuilder logStatement = new StringBuilder(10240);
                        logStatement.append("Threads Running at: ").append(now).append("\n\n\n");
                        Map<Thread, StackTraceElement[]> threads = Thread.getAllStackTraces();
                        List<Thread> sortedThreads = new ArrayList<Thread>(threads.keySet());
                        Collections.sort(sortedThreads, new Comparator<Thread>() {
                            @Override
                            public int compare(Thread o1, Thread o2) {
                                return o1.getName().compareTo(o2.getName());
                            }
                        });
                        for (Thread t : sortedThreads) {
                            logStatement.append("\tThread: name=").append(t.getName()).append(", id=")
                                    .append(t.getId()).append(", priority=").append(t.getPriority())
                                    .append(", state=").append(t.getState());
                            logStatement.append('\n');
                            for (StackTraceElement stackTraceElement : threads.get(t)) {
                                logStatement.append("\t\t" + stackTraceElement).append('\n');
                            }
                            logStatement.append('\n');
                        }
                        w.write(logStatement.toString());
                        w.close();
                    } catch (IOException ex) {
                        LOG.error("Unable to write the ProcessWatch output file: " + logFile.getAbsolutePath(),
                                ex);
                    }
                    try {
                        Thread.sleep(sleepPeriod);
                    } catch (InterruptedException ex) {
                        LOG.error("woken up during sleep of the ProcessWatch thread", ex);
                    }
                }
            }
        };
        processWatchThread = new Thread(processWatch, "ProcessWatch thread");
        processWatchThread.setDaemon(true);
        processWatchThread.start();
    }
}

From source file:ThreadLog.java

/**
 * Registers the calling thread with the ThreadLog. Logging calls to
 * ThreadLog will be logged to the specified log file if their priority is
 * equal to or higher then the specified log level. If 'logFile' is equal to
 * 'null', messages will be printed to System.err.
 * /*w w  w  .  j av a2s. c  o  m*/
 * @param logFile
 *        The file to log to, or <tt>null</tt> to log messages to
 *        System.err.
 * @param logLevel
 *        One of the constants ERROR, WARNING, STATUS, TRACE, or ALL.
 * @see #ERROR
 * @see #WARNING
 * @see #STATUS
 * @see #TRACE
 * @see #ALL
 */
public static void registerThread(String logFile, int logLevel) {
    ThreadLog threadLog = _createThreadLog();

    Writer logWriter = null;
    try {
        logWriter = _getLogWriter(logFile);
    } catch (IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();

        try {
            logWriter = _getLogWriter(null);
        } catch (IOException ignore) {
            // ignore
        }
    }

    threadLog.setLogWriter(logWriter);
    threadLog.setLogLev(logLevel);

    Thread currentThread = Thread.currentThread();
    threadLog.setThreadName(currentThread.getName());
}

From source file:com.vodafone360.people.service.transport.http.HttpConnectionThread.java

public static void logW(String tag, String message) {
    if (Settings.ENABLED_TRANSPORT_TRACE) {
        Thread t = Thread.currentThread();
        Log.w("(PROTOCOL)", tag + "[" + t.getName() + "] ################### " + " : " + message);
    }//  w w w  . j  a v a2  s. co  m
}

From source file:com.vodafone360.people.service.transport.http.HttpConnectionThread.java

public static void logI(String tag, String message) {
    if (Settings.ENABLED_TRANSPORT_TRACE) {
        Thread t = Thread.currentThread();
        Log.i("(PROTOCOL)", tag + "[" + t.getName() + "]" + " : " + message);
    }/*from w w  w.  j a va 2s. co m*/
}

From source file:com.vodafone360.people.service.transport.http.HttpConnectionThread.java

public static void logD(String tag, String message) {
    if (Settings.ENABLED_TRANSPORT_TRACE) {
        Thread t = Thread.currentThread();
        Log.d("(PROTOCOL)", tag + "[" + t.getName() + "]" + " : " + message);
    }//from   ww w. j a v  a2 s. c om
}