I have a Java class in AEM called "helper", the snippet looks like this:
public class Helper extends Thread{
................
private String glossaryText;
public String getGlossaryText() {
Thread thread = new Thread() {
public void run() {
glossaryText = getGlossaryHTML();
}
};
try {
thread.start();
}
catch(Exception e) {
}finally {
//this does NOT stop the thread
thread.interrupt();
}
System.out.println(thread.isAlive());
return glossaryText;
}
........
}
The problem is that, no matter what i do, System.out.println(thread.isAlive()); always print "true". thread.interrupt(); did not stop the thread.
interrupt()
does not work instantly stop the thread. It is just a signal to the thread, that it should stop itself soon. You have to build an reaction to it yourself.
The thing is, you don't wait for your thread to end. You just start the thread, signal it to stop an then check if its alive. There is a really small chance that between thread.start()
and your print the thread will be executed, but in most cases the executing thread will just end processing getGlossaryText()
.
You could just put a thread.join(500);
in front of your print and then your thread won't be alive anymore because it waits for the thread to be executed.