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.");
}
}