Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Quick Questions on Syntax (http://www.velocityreviews.com/forums/t616917-quick-questions-on-syntax.html)

Miles 05-26-2008 02:37 PM

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.

Arne Vajhøj 05-26-2008 02:54 PM

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

Lord Zoltar 05-26-2008 02:59 PM

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).

Arne Vajhøj 05-26-2008 03:20 PM

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

john 05-26-2008 03:30 PM

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.

Daniele Futtorovic 05-26-2008 04:02 PM

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"

Daniel Pitts 05-26-2008 04:03 PM

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/>

Tom Anderson 05-26-2008 04:04 PM

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

Tom Anderson 05-26-2008 04:06 PM

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

Miles 05-26-2008 04:12 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57