Re: Quick Questions on Syntax
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
---910079544-1218510002-1211817911=:10288
Content-Type: TEXT/PLAIN; CHARSET=iso-8859-1; FORMAT=flowed
Content-Transfer-Encoding: 8BIT
Content-ID: <Pine.LNX.4.64.0805261705191.10288@urchin.earth.li>
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.
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.
tom
--
And dear lord, its like peaches in a lacy napkin. -- James Dearden
---910079544-1218510002-1211817911=:10288--