Tom Anderson wrote:
> On Mon, 26 May 2008, Arne Vajhøj wrote:
>> Lord Zoltar wrote:
>>> Umm I think you are talking about the lines that look like:
>>> (new Thread(new Producer(drop))).start();
>>> ...correct?
>>> To me it looks like they are creating a new Thread object and calling
>>> start() on it, without assigning the new object to a variable. I am
>>> pretty sure this is legal, but I don't think you can reference the
>>> object that gets created here after it's been created (since you have
>>> nothing to reference it by) so I'm not sure what the point of this way
>>> of doing thins is. This syntax is not something I see very often, and
>>> I'm not sure I see a point to it, except maybe for brevity for simple
>>> examples.
>>> Maybe someone who has a non-trivial example of the way to use this can
>>> correct me? It might be an accepted practice for working with threads
>>> in Java, although it's been a while since I've done Java threads (and
>>> I never saw this syntax back then).
>>
>> It is most certainly valid syntax.
>>
>> The problem is that it is not possible to join on the started thread
>> (or in other ways interact with it).
>>
>> If that is not needed, then it can be used.
>>
>> I don't think it is a construct used in many serious programs.
>
> Maybe there's no need to interact with the threads from the thread which
> creates them. That wouldn't be that surprising. In that case, it's
> cleaner not to keep a reference.
It can happen.
But I do find wild running threads to be somewhat suspicious code.
If you need to start something, then you will usually want to know
if it is done.
> Bear in mind that the threads share a reference to the Drop object, and
> it's straightforward for them to interact with each other through that.
Not as straightforward as having a ref to the Thread.
Arne
|