Removing Threads from JVM
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Becky_Givner
Posted On:   Monday, March 26, 2001 10:44 AM

I am running Apache Jserv on a Linux platform, with automatic class-reloading. I have a singleton class called Scheduler.java that runs as a Thread, and a few servlets that get a handle to this thread. public class Scheduler extends Thread { private static Scheduler scheduler; public static synchronized Scheduler getInstance(){ if (scheduler == null){ scheduler = new Scheduler(); scheduler.start(); } return scheduler; } The problem I have is that if I recompile either the Scheduler class or the servlet   More>>

I am running Apache Jserv on a Linux platform, with automatic class-reloading.




I have a singleton class called Scheduler.java that runs as a Thread, and a few servlets that get a handle to this thread.






public class Scheduler extends Thread {



private static Scheduler scheduler;




public static synchronized Scheduler getInstance(){



if (scheduler == null){


scheduler = new Scheduler();


scheduler.start();


}


return scheduler;



}





The problem I have is that if I recompile either the Scheduler class or the servlets that get an instance of it,
I end up with multiple instances of Scheduler running.



Even if I DELETE all the classes and recompile them, I still have old threads running somewhere in the background.



The only way to get rid of them is to stop and restart the web server.



I would like to know why the threads are still running ( I did NOT do setDaemon(true), if that matters)
and how to stop them.



Also, setting scheduler = null does not stop the thread from running.

   <<Less

Re: Removing Threads from JVM

Posted By:   Alex_Chaffee  
Posted On:   Monday, April 30, 2001 05:11 PM

This is a JServ issue. If you change a class' definition (using auto-reload), then old versions are not automatically deallocated. This is actually a limitation of Java's classloader.


Restarting your server is the correct thing to do. You shouldn't be loading new versions of classes on a production box anyway -- just during development.

Re: Removing Threads from JVM

Posted By:   Edward_Harned  
Posted On:   Tuesday, March 27, 2001 11:39 AM

First, there is no automatic "Java" way to stop a thread. You must build a method into the class to recognize a stop situation. There once was a stop() method but it never worked well and has since been deprecated. There are operating system commands to stop a thread such as: kill -9 . You can always write you own, but the problem is getting the waiting Java thread to activate. JavaWorld.com had a series on threads with ways to stop an executing thread and there are many books on the subject.



Next, the Singleton code you mention does not work. Again, JavaWorld.com had an excellent article on why this is so. It is too involved to duplicate here.



Simply go to http://www.javaworld.com and search the articles for singleton and for threads.

About | Sitemap | Contact