• Post Reply Bookmark Topic Watch Topic
  • New Topic

does thread eligible for garbage collect when stopped?  RSS feed

 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I stop a thread using .stop() method, will it be eligible for garbage collection?
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:If I stop a thread using .stop() method, will it be eligible for garbage collection?


First, the Thread's stop() method is deprecated. Don't use it.

Second, the Garbage Collector is for objects -- and not "a thread". Or perhaps, you meant the Thread object that is used to represent the thread?


Anyway, when stop() is called, a ThreadDeath object is used to unroll the thread stack. And when the stack is completely unrolled, and assuming that it is successful, the state of the thread is declared as "terminated", and it is all cleaned up. This means that any reference to the Thread object, along with possibly lots of other objects, referred to by local variables on the stack, are now gone.

And as long as no other part of the program is referring to the now terminated Thread object, yes, the object becomes eligible for garbage collection.

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object is eligible for garbage collection if there are no live threads that hold any reference to it.

This rule holds for any kind of object, there's nothing special about Thread objects that would make it any different.
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. Tes sir, you are correct, I am referring to thread objects. But how can I stop the thread object?

Sir, can you please give me idea on how to accomplish this:
I have a singleton thread object in my application and the user has option to start, suspend, and stop the said thread object.

While starting, the thread will accept parameter from user and use the value while the thread is running.
While suspend, just suspend the thread.
When stopped. It will discard the the thread/user input value when the thread was started and be ready for the next user pressing the start button.

I've build a singleton thread class and tt's easy to start and suspend the thread but I'm not sure how can I stop it
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote: . . . But how can I stop the thread object?
. . .
You don't.

If the thread has a loop, you arrange for the loop to terminate and the thread will terminate gracefully.
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry sir but I don't understand. Does it mean suspending and stopping the thread are the same and no use for .resume() in thread??

My thread object has actually no loop but the object inside the thread method (it instantiates an object) has the loop which executes the process.

But how can I pass a new value from the thread in case I stop-start the singleton thread?
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread#suspend and Thread#stop are both flawed methods which can cause dangerous errors and should never be used. If you can't use suspend you can't use resume. Have you never read this?
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the documentation you provided.
I tried implementing it to my app but the start() which instantiates the blinker thread is not being invoked. here's my code:



This is how I call my create a thread:



After debugging, I found out that the start() is not being invoked thus blinker is null so my the process under run() does not enter while loop. I also tried placing start() inside run() but the thread created is not equal to thisThread. Can someone please help me
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:
After debugging, I found out that the start() is not being invoked thus blinker is null so my the process under run() does not enter while loop. I also tried placing start() inside run() but the thread created is not equal to thisThread. Can someone please help me


Why do you think that your start() method should be called? In other words, how is it supposed to be called? The thread system calls your run() method only.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!