On Tuesday 16 September 2008 03:55:18 Brian Candler wrote:
> > I'd like to detect "no work left" as a case of the "farmer" going out of
> > scope, if possible.
> 
> In principle, if the Farmer is an object, then it can have a finalizer 
> which sends the terminate message to all the workers. The finalizer 
> won't be called immediately when the Farmer goes out of scope, but at 
> some time later.

That's fine. As long as "some time" is reasonably soon, I'm not intending to 
assume anything about when the workers are terminated.

Ideally, I would be able to trigger collection manually.

> In practice, it isn't easy to do.

That's fine. I intend to solve this once, and I don't mind a challenge!

> The finalizer is only called *after*  
> the farmer has been destroyed, so you can't use any methods or instance 
> variables on it.

However, wouldn't it be scoped? I could as easily set a local variable when I 
create the finalizer, right?



Still doesn't answer the other part I need: Weak references. I need to know 
exactly how I can use them, and how I can't -- what might be dangerous about 
them, for example.

Looking at your example, your workers still have references to your queue and 
to the farmer, which means the farmer will never be collected.