0

I created a utility class for getting info about the running threads. one thread called MQTT_THREAD starts when a button is pressed. and i have another button named play, when pressed it should check first if the thread MQTT_THREAD exists or not or ,in other words, was born or not.

At run time, i press the button that starts the MQTT_THREAD, and when I press the play button, it displas that the thread MQTT_THREAD is not existing. I believe its mostl because my ack of understaning threads or a small bug in the logic. Below is my code.

Kindly please have a lok at it and let me know what i am missin.

Code_utitlity methods used

public static Thread[] getAllRunninthreads() {
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
    return threadArray;
}
public static boolean isThreadExist(String threadName) {
    boolean isExist = false;
    for (int i = 0; i < ThreadsUtility.getAllRunninthreads().length; i++) {
        if (ThreadsUtility.getAllRunninthreads()[i].getName().equals(threadName)) {
            return (isExist = true);
        }
    }
    return isExist;
}

Code_at the main thread:

 if (e.getSource() == Bplay) {
         if (!ThreadsUtility.isThreadExist(MQTT_THREAD)) {
             System.out.println(MQTT_THREAD + " is not existing.");
         }else {
             System.out.println(MQTT_THREAD + " exists.");
             if (!ThreadsUtility.isThreadExist(FILE_THREAD)) {
                 System.out.println(FILE_THREAD + " is not existing.");
             }else {
                 System.out.println(FILE_THREAD + " exists.");

             }
         }
rmaik
  • 1,076
  • 3
  • 15
  • 48

2 Answers2

0

Try calling getAllRunninThreads just once as calling it again and again would not give you consistent value of set/array (imagine creating new thread or exiting a thread) and hence would create issue.

public static boolean isThreadExist(String threadName) {
Thread[] threads = ThreadsUtility.getAllRunninthreads();
    for (int i = 0; i < threads.length; i++) {
        if (threads[i].getName().equals(threadName)) {
            return true;
        }
    }
    return false;
}

This is what api has to say for getAllStackTraces method

 A zero-length array will be returned in the map value if the virtual machine has no stack trace information about a thread.
SMA
  • 36,381
  • 8
  • 49
  • 73
  • thank you for the answer, but what is wron in my code – rmaik Dec 11 '14 at 12:15
  • 1
    ok, but when I run ThreadsUtility.getAllRunninthreads(); method it ives me all the threads available but the one I stared? is it because the thread is terminated when it finishes its work? – rmaik Dec 11 '14 at 12:29
  • Yes if your thread terminated may be gracefully or some exception then definitely you wont get to see it by above code as above code is just meant to show you live threads. – SMA Dec 11 '14 at 13:17
0

The Thread.getAllStackTraces().keySet(); displays all the available threads n the stack, but not the TERMINATED ones. So, i think your "mqtt thread" might not making heavy work that consumes time, therefore, by the time you press the "play" buton the thread might have finished its work and hence, TERMINATED and would not be listed in the Thread.getAllStackTraces().keySet()

Amrmsmb
  • 1
  • 27
  • 104
  • 226