• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Que on threads

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This question is from one of the mock exam.
Assume that th is an instance holding a thread object. th.start() causes the thread to start running and eventually complete its execution. The object reference by th is not accessable any more and is garbage collected when the garbage collecter runs
True/False.
The answer given is false. How is this possible? any explanation?
-sanjana
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

After the thread completes its execution, it goes into dead state which means one cannot say th.start() again.
However this does not imply that the reference to the thread "th" has been lost. There would be always a valid reference to it which can be verified by calling any another method (except public void start()) defined in the Thread class or of the class for which "th" is an instance.
class myThread extends Thread{
public void run(){
System.out.println("Hey this realy runs!");
}
public void run_again(){
System.out.println("Hey this runs again!");
}
public static void main(String[] args){
myThread th = new myThread();
th.start();
th.run_again();
}
}
Appreciate the fact that the above code can produce the output in any one of following format:
A)
Hey this runs again!
Hey this realy runs!
B)
Hey this realy runs!
Hey this runs again!
This is because starting a thread just means that thread is set in the runnable mode and has not yet started running.
Hope this helps
Nikhil
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and, suppose you create an instance of a Thread class which
starts itself in the contstructor, e.g.:

then the thread "registers" itself so there is actually a reference to it somewhere, and the garbage collector cannot clean it up until the thread exits its run( ) and dies. This differs from the behaviour of ordinary objects.
Hope this helps as well,
Gian Franco
 
Gian Franco
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in my previous post the code example has a cripled main
method read 'public static void' as 'public static void main'
 
sanjana narayanan
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the question, the thread runs and completes its execution eventually. When the object referenced by the thread, is no more accessible it has to be garbage collected.
I would like to emphasis on the point, in the question it is mentioned that object referenced by the thread is not accessible anymore.
That's why i say that the answer is True.
If u don't agree with this, tell me when it would be garbage collected.
-sanjana
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sanjana,
It seems to me that the mock exam itself is flawed.


The object reference by th is not accessable any more and is garbage collected when the garbage collecter runs.


1. Like Nikhil said, a thread is dead does not mean it can not be accessed.
2. We never know beforehand exactly when an object is garbage collected. Different GC algorithm is used in different Java Runtime implementation. This means that even when GC runs, we don't know whether particular object that is no longer accessible will be garbage collected. So, instead of 'such object will be garbage collected at such time', the right terminology will be 'such object is eligible for garbage collection after such time'.
3. Also


Assume that th is an instance holding a thread object.


does not sound right to me, because th is not an instance, it is a reference to an instance of a thread object.
I am adding 2 and 3 to build a case questioning the accuracy of the mock exam.
So, if the question is worded like the following

Assume that th is a reference to a thread object. th.start() causes the thread to start running and eventually complete its execution. The object reference by th is then eligible for garbage collection.

then the answer will be definitely false.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic