Quick Questions on Syntax

Discussion in 'Java' started by Miles, May 26, 2008.

  1. Miles

    Miles Guest

    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.
     
    Miles, May 26, 2008
    #1
    1. Advertising

  2. Miles

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, May 26, 2008
    #2
    1. Advertising

  3. Miles

    Lord Zoltar Guest

    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).
     
    Lord Zoltar, May 26, 2008
    #3
  4. Miles

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, May 26, 2008
    #4
  5. Miles

    john Guest

    (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.
     
    john, May 26, 2008
    #5
  6. 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"
     
    Daniele Futtorovic, May 26, 2008
    #6
  7. Miles

    Daniel Pitts Guest

    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/>
     
    Daniel Pitts, May 26, 2008
    #7
  8. Miles

    Tom Anderson Guest

    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, May 26, 2008
    #8
  9. Miles

    Tom Anderson Guest

    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
     
    Tom Anderson, May 26, 2008
    #9
  10. Miles

    Miles Guest

    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.
     
    Miles, May 26, 2008
    #10
  11. Miles

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, May 27, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    3
    Views:
    374
  2. JKop
    Replies:
    11
    Views:
    895
  3. Ian Davies

    quick syntax question

    Ian Davies, Jun 17, 2006, in forum: HTML
    Replies:
    9
    Views:
    361
    Toby Inkster
    Jun 18, 2006
  4. Robocop

    quick newbie syntax question

    Robocop, Oct 20, 2008, in forum: Python
    Replies:
    5
    Views:
    256
    Steven D'Aprano
    Oct 20, 2008
  5. Johannes Bauer

    Quick nested loop syntax?

    Johannes Bauer, Nov 19, 2008, in forum: Python
    Replies:
    3
    Views:
    345
    alex23
    Nov 20, 2008
Loading...

Share This Page