I’ve had a look at the posts about destructors in Java and have learnt that Java does not have them but I am confused about what I need to do in my application if there is no such thing as a destructor.
My application allows a user to create a series of webcrawlers, which are held in an arraylist. Each Crawler has a progress panel gui showing pages crawled etc and enables the user to pause that crawler. However, the user might also want to “terminate” that crawler.
It should be noted that each instance of the Crawler class has an instance of a Scraper class, which in turn has an instance of a DatabaseConnection class.
What do I need to do to enable the user to press “Terminate” and for that Crawler, and its Scraper, and the Scraper’s DatabaseConnection, all to be “shut down” and removed from the system.
Any advice would be appreciated.
First of all you need for this to run on separate threads to work well (anything you see about Java GUI’s being unresponsive is because this is not heeded).
The trick then is to have the thread(s) doing things, regularly check that a given condition is still true, and let your cancel button go in and reset the condition so it is false (like setting a field, or calling a setKeepGoing(false) metod). When the threads then find out they have been asked to stop, they stop and clean up accordingly.