Garbage Collection and Threads

S

Shirish Rai

I have a class, say A which is runnable. When A is created it creates one
or more threads. Thus each of these threads have a reference to the
instance of A. Therefore when the client that created A is done using it,
the instance of A cannot be garbage collected.

Is there any way to notify the threads to exit when the thread that
created A is done using it.

Thanks.

Shirish.
 
S

Skippy

I have a class, say A which is runnable. When A is created it creates one
or more threads. Thus each of these threads have a reference to the
instance of A.

Only if you keep a reference floating around. If not, there is no reference
kept anywhere and they will be garbage-collected some time later.
 
C

Chris Smith

Shirish said:
I have a class, say A which is runnable. When A is created it creates one
or more threads. Thus each of these threads have a reference to the
instance of A. Therefore when the client that created A is done using it,
the instance of A cannot be garbage collected.

Is there any way to notify the threads to exit when the thread that
created A is done using it.

You'll have to take some explicit action at the end of the client that
creates A. That action could involve setting a boolean flag inside A or
interrupting the threads that are running A's code.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Skippy said:
Only if you keep a reference floating around. If not, there is no reference
kept anywhere and they will be garbage-collected some time later.

It seems clear that in Shirish's case, the threads are created to run
the instance of A in the first place, so yes they would reference it.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top