This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes Removing objects (Threads) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Removing objects (Threads)" Watch "Removing objects (Threads)" New topic

Removing objects (Threads)

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I'm trying to run something in the background that stops and restarts if a key is pressed. Immediately after the keyPressed() method (implementing the KeyListener interface) is invoked I set MyThread to null, instantiate it, and start() it. However, the original MyThread is still running. Why isn't the original one removed when MyThread is set to null? I'm even running a System.gc() to get rid of it. I basically just want one version of MyThread to be running at any one time.
Aleksey Matiychenko
Ranch Hand

Joined: Apr 03, 2001
Posts: 178
One suggestion.
If you only want one instance then implement a SingleTon by making all constructors for your thread private and creating a getInstance method that will always return the reference to the only instance of your thread class.
Then implement methods that would allow you to restart the thread.

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Thanks for the response. Do you know why the setting of MyThread to null is not removing it? I thought this was the standard way to get rid of an object.
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
Setting it to null only removes the reference to the object, not the object itself. This frees it up for garbage collection, but since there is no way to predict when the GC will run, it is not a reliable way to stop a thread. Running System.gc() will not force the GC to run either. I believe the standard way to stop a thread is to set a flag that gets checked in the run method. For example:

When you want to stop the thread, just set isRunning to false.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Actually the thread will not be GC'd as long as it is running, even if all other references in the program are deleted. This is because the way GC is defined, an object my be deleted only if there is no way to access it from any live thread. A live thread can always access itself; therefore it does not require that any other references to it exist in order to prevent garbage collection.
Also, in the last code example, it is important that all access to isRunning be from synchronized methods or blocks of code. Otherwise different threads may be using different, out-of-date copies of the isRunning value, without bothering to updateeach other. If this happens, setting isRunning to false may have no effect, since MyThread may never bother to get an updated value.
Typically, you also want to set up the code to check isRunning more than once while the thread is running. It depends on what the run() method is supposed to be doing in the first place, but usually if there's any sort of loop withing the run() method, you add (isRunning == true) to the list of conditions to continue the loop:
class MyThread {

private boolean isRunning;

public synchronized boolean getIsRunning() {
return isRunning;

public sunchronized void setIsRunning(boolean isRunning) {
this.isRunning = isRunning;

public void run() {
while (getIsRunning() && isThereMoreWorkToBeDone()) {


"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Removing objects (Threads)
It's not a secret anymore!