In My Application have a service class(MyServiceClass) which
- loads configuration file
- validates DB connection
- 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;
}
}