*
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 Android Security Essentials Live Lessons this week in the Android 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: 14074
    
  16

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: 4344
    
    8

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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Interthread communication program doubt
 
Similar Threads
static synchronized method
static synchronized method
Thread wait() and notify()
Explain me this output (notify)
Concurrency Utilities