2

I have error in my applications server logs

SEVERE: A web application appears to have started a thread named [MyThread] but has failed to stop it. This is very likely to create a memory leak.

To resolve this i should stop the thread in contextDestroyed method of My ServletContextListener implementation.

But I am not able to understand how to get reference of my Thread/Runnable so that i can call interrupt().

One solution i have is : putting this thread instance in ServletContext attribute but not sure it is good practice or not. Please suggest if you follow some other approach in your application.

Vipin
  • 4,851
  • 3
  • 35
  • 65
  • Have a look at [this answer](http://stackoverflow.com/a/785802/3080094). – vanOekel Feb 28 '14 at 16:53
  • It's not helpful :( , I need some best practice – Vipin Feb 28 '14 at 17:25
  • Whoever started it should shut it down. – David Ehrmann Mar 03 '14 at 04:39
  • @DavidEhrmann This is a old struts application , while debugging this issue i found it is started in contextInitialized method of My ServletContextListener implementation. – Vipin Mar 03 '14 at 04:41
  • one idea that might help you is to put names on all of your threads, that will help you discover who started that thread. – msknapp Mar 03 '14 at 04:55
  • @msknapp I know it is started in contextInitialized at the start of application , how the question is how can i retain thread reference to call thread.interrupt() method. – Vipin Mar 03 '14 at 05:01
  • not sure, is it possible to add a static reference to it in one of your classes? – msknapp Mar 03 '14 at 05:13
  • @msknapp , yes. Actually I am looking for some best practice, this is my first webapp so want to know what people follow generally. – Vipin Mar 03 '14 at 05:18
  • You have a ServletContextListener. This listener is called at startup (where it starts the thread), and at shutdown. So just keep a reference to the thread in a field of the listener, and interrupt the thread in the shutdown callback method. – JB Nizet Mar 03 '14 at 07:20
  • @JBNizet yes this is indeed a easy and good approach. Although not feasible in my case as i am working on old application and to start the thread from ServletContextListene i have big stack of methods/classes. put your comment as answer also , it will help everyone lookig at this question. – Vipin Mar 04 '14 at 04:57

1 Answers1

0

In My Application have a service class(MyServiceClass) which

  1. loads configuration file
  2. validates DB connection
  3. initializes this thread

At the start of application its isConfigurationValid() method is called from ServletContextListener and this method does all above 3 operations.

This class has a instance variable of my Thread type , which is assigned Thread object inside its private initializeThread method. This method is using double check idiom to ensure only one Runnable object is created.

Now I have provided a static getter method for my thread instance field and i interrupt my Thread using this in contextDestroyed method. Below is raw code , please ignore syntax errors if any

MyServiceClass {

    Thread thread;

    public static boolean isConfigurationValid(){
        loadConfigFile(); // private method of same class
        validateDBConfiguration();// private method of same class
        initializeThread();// private method of same class
    }

    private static void initializeThread (boolean usePrimaryPort) {
        {
            if (thread == null){
                synchronized (MyServiceClass.class){
                    if (thread == null){
                        Thread thread = new Thread();
                        Thread.start();
                    }
                }
            }
            return;
        }
    }
    public static Thread getThread(){
        return thread;
    }
}
Vipin
  • 4,851
  • 3
  • 35
  • 65