do we need separate thread for FirstThread
Thu Feb 8 16:58:00 GMT 2001
Cedric Berger <email@example.com> writes:
> Per Bothner wrote:
> > Bryce McKinlay <firstname.lastname@example.org> writes:
> > > > * Avoid creating threads in single-threaded programs.
> > >
> > > Well, we can't really do this, since a separate finalization thread (for
> > > example) will be required in order to implement finalization correctly.
> > I'm missing something here. The JLS says finalizer are run in an
> > "unspecified" thread. There seem to be two logical alternatives:
> > (1) a special finalizer or gc thread, or
> > (2) the thread that causes gc to be invoked (by doing a new).
> > Why is (2) prohibited?
> (2) is prohibited in a multithreaded application because of risks
> of deadlock.
Could you explain why? If finalizers are triggered by gc which is
triggered by new, then the thread that did a 'new' isn't going
anywhere until gc is done. I guess I could conceive of a finalizer
waiting for a resource being held by the thread doing the 'new',
and the deadlock could be avoided by running the finalizers in a
separate thread, return from gc (and 'new') while the finalizers
run in the background. But why? Is there any non-bogus program
that could benefit from this? And is there anything in the specs
(or otherwise) that prohibits (2)?
More information about the Java