This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Interthread communication program doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Interthread communication program doubt" Watch "Interthread communication program doubt" New topic
Author

Interthread communication program doubt

Abhay Saini
Greenhorn

Joined: Jun 15, 2011
Posts: 19

Hey can anyone please tell me :
1. At line 5, which q are these?
2. Why is, at line 6, no object is instantiated? Directly the start function is called?
Thanks...




PS :
Then
calling
new Producer(q);
would be exactly the same as
Producer re = new Producer(q);
If so, then the 2nd way is useful for?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

Welcome to the Ranch!

1. The first q (this.q) is the member variable declared in line 2. The second q is the argument variable from line 4. So, you're setting the member variable to the value of the argument variable.

2. An object is instantiated. Why do you think there isn't? (A variable is not the same as an object). There's no variable, instead the start() method is called directly on the new Thread object. This code does exactly the same as:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Abhay Saini
Greenhorn

Joined: Jun 15, 2011
Posts: 19

Thanks!


Then
calling
new Producer(q);
would be exactly the same as
Producer re = new Producer(q);
If so, then the 2nd way is useful for?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

The second approach is what you need to do if you want to keep a reference to the new object. With the first approach you create a new object, but you can't then call any methods on it later in the method.

For example:
or
Abhay Saini
Greenhorn

Joined: Jun 15, 2011
Posts: 19
Thanks
Matthew!
 
Don't get me started about those stupid light bulbs.
 
subject: Interthread communication program doubt
 
Similar Threads
Explain me this output (notify)
static synchronized method
Thread wait() and notify()
static synchronized method
Concurrency Utilities