Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads and Garbage Collection  RSS feed

 
Gaz Williams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was wondering what happens to Threads that are running but are no longer reference from the stack. If I was to run the following pseudo code (I haven't compiled it), what would happen to the Thread running in ThreadTester? Would the JVM terminate it?

 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaz Williams wrote:If I was to run the following pseudo code (I haven't compiled it), what would happen to the Thread running in ThreadTester

And why wouldn't you simply 'try it'? Then you needn't guess nor ask somewhere. Of course, the Thread runs until it leaves it's run() method.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, there is a internal reference to the thread(ThreadTester), some thing like, thread Id, name or what ever.

Let's see what other say!
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaz Williams wrote:Hi All,
I was wondering what happens to Threads that are running but are no longer reference from the stack. If I was to run the following pseudo code (I haven't compiled it), what would happen to the Thread running in ThreadTester? Would the JVM terminate it?


All threads can access its own thread object via the currentThread() method call. So, the thread object for ThreadTester is reachable from the thread tester scope, and held internally so that it can call the currentThread() method to obtain it.

Henry
 
Gaz Williams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, from what you are saying, the ThreadTester object isn't garbage collected and the Thread will continue to run...

Thanks for the help
 
Gaz Williams
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here is an example that would help check if the thread is running.....http://faq.javaranch.com/java/ThreadLister
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All threads can access its own thread object via the currentThread() method call. So, the thread object for ThreadTester is reachable from the thread tester scope, and held internally so that it can call the currentThread() method to obtain it.

Henry

Henry, I couldn't get that point. Please explain! Thanks

By making the variable t as null, is that 'Thread' object is eligible for GC? And what about the thread object ThreadTester, which is created in the 7th line in the OP's code?

I think, those two object are not eligible for GC? Is that correct?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!