File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Two queries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Two queries" Watch "Two queries" New topic

Two queries

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
I have a question and a query.
question: does yield() hold all locks?
query: in ThreadGroup we can set a ThreadGroup to be of "daemon" type. now, api says that if a ThreadGroup is of "daemon" type then it will be destroyed only when all of it's member threads are finished. My question is what do we mean by "destroy" on ThreadGroup?? Does it mean that the ThreadGroup object becomes eligible for GrabageCollection?
Well, if that is the case then what happens in following code,
ThreadGroup tg = new ThreadGroup();
// code of many threads and their binding
// to the above threadgroup "tg"
// all the member threads are started
tg = null; // now at this point we made tg
// explicitly "null" to make it
// eligible for GC but suppose
// that some member threads are still
// running, then what happens??
hope i am clear in explaining my problem.
Suresh Bansal
Ranch Hand

Joined: Jul 23, 2001
Posts: 91
yeild() will allow other same priority threads to run.
For daemon threads.
If the program has no more non daemon threads, the system exits and destroys daemon threads in the process. The presence of daemon threads does not stop the program from exting.
I think, that setting threadgroup to null will not destroy the threads in it.

Journey is as important as the Destination.<br /> <br />PMP, SCJP, SCJD, SCEA (Part 1), SCMAD, SCDJWS
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
yield does not release locks.
After a while I have managed to produce this code that shows, I hope, that a daemon threadGroup is only garbage collected when its threads have finished. However a non daemon one it is not garbage collected even then.

SCJP2. Please Indent your code using UBB Code
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Jose,
I guess you are right.
And one more thing, instead of writing "threadgroup is destroyed ONLY when all of it's member threads are finished" I should have said "threadgrou is Destroyed when all of its member threads are finished" for Daemon Threadgroups (to be precise as in API).
That makes the same suggestion you made, we can guarantee about destroyal of a daemon ThreadGroup when all of i'ts member threads are finished but we can't be sure the same about the non-daemon threadgroup. makes sense.
Btw, I am impressed that you could come upwith a code particularly because I know little about those lang.ref package classes so I couldn't have come up with similar example myself.
I agree. Here's the link:
subject: Two queries
It's not a secret anymore!