• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

A beginner's gc Question need your help!!!!

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
a)true.
B)false

the given as:B.

I think when the system run the gc,it will certain collect the object that unreachable(unmarked) object.
But we are not sure whether the system will run the gc,or when the system will run gc.
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think that the while you still have a reference to that object it will not be gc.
public class TestThread extends Thread{
public void run() {
System.out.println("Thread started");
}
static void alive() {
System.out.println("The object is alive");
}
public static void main(String args[]) {
TestThread t = new TestThread();
t.run();
try
{
t.join();
}
catch (InterruptedException ex)
{
}
System.out.println("The thread is alive:" + t.isAlive());
t.alive();
}

}
.. Cristian
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James:
The correct ans is B because th is indeed still holding the object.To be collected th = null
Hope this help
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add my $.02, yes the answer is B, because, as has been said, even after the Thread is done with its run method there is still a reference to.
In fact after it is done with run() you can still call a Threads methods and access its variables, you just cant start it running again.
------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Dave Vick (edited November 01, 2001).]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Dave......

In fact after it is done with run() you can still call a Threads methods and access its variables, you just cant start it running again.


dave, can u just tell me why the Threads are implemented that way? why is it that we can call other methods but we can't call Start()? whats that specific reason that Thread has been implemented this way? what harm it cause if i start the thread all over again if it has completed execution?
thx in advance ........ Ajit
 
Cristian Negresco
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
And in the code, at the line 10:
t.run();
must be replaced by
t.start();
otherwise the run() is just a method like any other method and there is no multithreading there.
A mistake that is useful I think...
..Cristian
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic