File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Removing objects (Threads)

 
Paul Keohan
Ranch Hand
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Thanks.
Paul
 
Aleksey Matiychenko
Ranch Hand
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Paul
 
Angela Lamb
Ranch Hand
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
<code><pre>
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()) {
doSomeWork();
}
}

...
}
</pre></code>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic