1

I'm working on struts 2 framework. I've a requirement as when user hits a specific url i need to enable a functionality and after some minutes have to disable the same process. All these will be running at background when hitting the url. So I've decided to go with Threads for it. Following is the code I'm using...

public class ActDeactTrace extends Thread {
 @Override
public void run() {
    System.out.println("RUNNING THREAD...BEFORE SLEEP");
    enableTrace();
    try {
        sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("RESUMED THREAD .... AFTER WAKE UP");
    disableTrace();
    super.run();
}
public void enableTrace(){

}
  public void disableTrace(){

}
}

When the action is invoked from the user i just start the Thread through its object. Since it is a web app, many users can do the same. So should i manually destroy the created thread after the disableTrace method is executed to avoid JVM problems or will it automatically get destroyed by garbage collectors.

Ram
  • 845
  • 1
  • 13
  • 26

2 Answers2

0

Once the thread is finished running it can be garbage collected (if there are no references to the Thread object). A running Thread cannot be garbage collected.

See Java Thread Garbage collected or not

Community
  • 1
  • 1
KyleM
  • 4,445
  • 9
  • 46
  • 78
0

Worth having a read of this related question. Threads need to be carefully managed when you're running an application in a Java EE application server. It's recommended that you don't start and attempt to destroy threads in your application code.

Community
  • 1
  • 1
Alex Barnes
  • 7,174
  • 1
  • 30
  • 50