• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Removing objects (Threads)

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic