Quick Questions on Syntax

M

Miles

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

Arne Vajhøj

Miles said:
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
 
L

Lord Zoltar

Miles said:
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.

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

Arne Vajhøj

Lord said:
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
 
J

john

(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.
 
D

Daniele Futtorovic

(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. ;)
 
D

Daniel Pitts

john said:
(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.
 
T

Tom Anderson

(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
 
T

Tom Anderson

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
 
M

Miles

Tom said:
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.
 
A

Arne Vajhøj

Tom said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top