Returns a formatted string representing the thread identified by the passed id - Java Thread

Java examples for Thread:Thread

Description

Returns a formatted string representing the thread identified by the passed id

Demo Code

//package com.java2s;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

public class Main {
    public static void main(String[] argv) {
        long id = 42;
        System.out.println(formatThreadName(id));
    }//from  www.  jav a2 s.c  o m

    /** The ThreadMXBean */
    protected static final ThreadMXBean tmx = ManagementFactory
            .getThreadMXBean();

    /**
     * Returns a formatted string representing the thread identified by the passed id
     * @param id The id of the thread
     * @return the formatted message
     */
    public static String formatThreadName(long id) {
        if (id < 1)
            return "[Nobody]";
        ThreadInfo ti = tmx.getThreadInfo(id);
        if (ti == null)
            return String.format("No Such Thread [%s]", id);
        return String.format("[%s/%s]", ti.getThreadName(),
                ti.getThreadId());
    }
}

Related Tutorials