![]() |
Quick Questions on Syntax
Hi all, Reading over threads in the Concurrency trail on Sun's tutorials and noticed the following: public class ProducerConsumerExample { public static void main(String[] args) { Drop drop = new Drop(); (new Thread(new Producer(drop))).start(); (new Thread(new Consumer(drop))).start(); } } Curious as to why the presence of the outer parenthesis? Is the line casting the Producer as a "new thread". If so, why is it necessary instead of just using new by itself? Is it because the object is instantiated directly and not being assigned to a variable? Thanks for clarification. -- Miles Note: Not new to programming, just new to Java. |
Re: Quick Questions on Syntax
Miles wrote:
> Reading over threads in the Concurrency trail on Sun's tutorials and > noticed the following: > > public class ProducerConsumerExample { > public static void main(String[] args) { > Drop drop = new Drop(); > (new Thread(new Producer(drop))).start(); > (new Thread(new Consumer(drop))).start(); > } > } > > Curious as to why the presence of the outer parenthesis? Is the line > casting the Producer as a "new thread". > > If so, why is it necessary instead of just using new by itself? Is it > because the object is instantiated directly and not being assigned to a > variable? As I read ut then you new a Runnable and then new a Thread with that. The outer parenthesis'es are not needed by I also always put them there to clearly indicate what the method call is on. Arne |
Re: Quick Questions on Syntax
Miles wrote: > Hi all, > > Reading over threads in the Concurrency trail on Sun's tutorials and noticed the > following: > > public class ProducerConsumerExample { > public static void main(String[] args) { > Drop drop = new Drop(); > (new Thread(new Producer(drop))).start(); > (new Thread(new Consumer(drop))).start(); > } > } > > Curious as to why the presence of the outer parenthesis? Is the line casting > the Producer as a "new thread". > > If so, why is it necessary instead of just using new by itself? Is it because > the object is instantiated directly and not being assigned to a variable? > > Thanks for clarification. > > -- > > Miles > 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). |
Re: Quick Questions on Syntax
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. But for all kinds of of quick write, run and done situations it is used. Arne |
Re: Quick Questions on Syntax
(new Thread(new Producer(drop))).start(); I don't see there any confusing in this statement. What Lord had said is right. Here just new Object thread created and it is made to start execution by instanting start(). Question is why there is need to outer Paranthesis. Skipping paranthesis we can do like this..we need to initialize thread here as Thread td=new Thread(new Producer(drop)); td.start(); We need to take into acc the whole object so paranthesis is needed. |
Re: Quick Questions on Syntax
On 2008-05-26 17:30 +0100, john allegedly wrote:
> (new Thread(new Producer(drop))).start(); > > I don't see there any confusing in this statement. What Lord had > said is right. Here just new Object thread created and it is made to > start execution by instanting start(). Question is why there is need > to outer Paranthesis. The outher paranthesis are not necessary, syntactically. They're a matter of preference, of making the code more readable (arguably). Alternatively, you could argue the author was in doubt. ;) -- DF. to reply privately, change the top-level domain in the FROM address from "invalid" to "net" |
Re: Quick Questions on Syntax
john wrote:
> (new Thread(new Producer(drop))).start(); > > I don't see there any confusing in this statement. What Lord had > said is right. Here just new Object thread created and it is made to > start execution by instanting start(). Question is why there is need > to outer Paranthesis. > > Skipping paranthesis we can do like this..we need to initialize thread > here as > > Thread td=new Thread(new Producer(drop)); > td.start(); > > We need to take into acc the whole object so paranthesis is needed. new Thread(new Producer(drop)).start() works just as well. Parenthesis were probably added for clarity, definitely not required. -- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/> |
Re: Quick Questions on Syntax
On Mon, 26 May 2008, Miles wrote:
> (new Thread(new Producer(drop))).start(); > (new Thread(new Consumer(drop))).start(); > > Curious as to why the presence of the outer parenthesis? They're unnecessary. This: new Thread(new Producer(drop)).start() ; Would work just as well. I don't know why the coder added them. tom -- And dear lord, its like peaches in a lacy napkin. -- James Dearden |
Re: Quick Questions on Syntax
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 |
Re: Quick Questions on Syntax
Tom Anderson wrote:
> On Mon, 26 May 2008, Miles wrote: > >> (new Thread(new Producer(drop))).start(); >> (new Thread(new Consumer(drop))).start(); >> >> Curious as to why the presence of the outer parenthesis? > > They're unnecessary. This: > > new Thread(new Producer(drop)).start() ; > > Would work just as well. I don't know why the coder added them. > > tom > Thanks to all for the response. I'm just getting familiar with the syntax and the outer perens threw me off a little. Thanks again. -- Miles Note: Learning Java, coming from Delphi/ObjectPascal. |
| All times are GMT. The time now is 04:42 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.