wood burning stoves*
The moose likes Beginning Java and the fly likes Confusion in threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confusion in threading" Watch "Confusion in threading" New topic
Author

Confusion in threading

Shiv Tattva
Greenhorn

Joined: May 07, 2014
Posts: 6
Im new to java and I have started learning this chapter on threads which left me with number of doubts.



In program 2--->
At 43rd line , thread1 ( object of class MyRunnable) is passed as argument to the Thread constructor . So 43rd line creates a thread object.

In program 1--->
Similarly at 8th line, "this" is the object of Mythread class right ?

Why in program 1 , Thread object and Mythread objects are created inside Mythread class while in program 2, Thread and MyRunnable objects are created inside the ThreadTest class (which contains main method). Why not create both the objects in any one class?


Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 252
    
    4
Perhaps that's just the way the authors wrote it? Functionally they are equivalent (a Thread is created with a Runnable in both cases).

Personally the first way does not make much sense to me. There isn't much point in a class implementing Runnanble if it is going to create its own Thread.

What book did you get that code from?
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 234
    
    1

Line 45 looks errant. thread1 is of type Runnable, which doesn't have a start() method. Maybe it's supposed to be:



To be honest, this isn't very pretty code, and I wouldn't try to derive much meaning from it if I were you.
Shiv Tattva
Greenhorn

Joined: May 07, 2014
Posts: 6
First of all, thank you both for replying.
@Joel Christophel (You are right) Im sorry , it was my typing mistake.
@Mike.J.Thompson It's from my friend's handwritten notes .
Am I right about "this"?
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2249
    
  47
Shiv Tattva wrote:Am I right about "this"?

If you are referring to line 8 then yes you are correct to pass 'this' as the parameter.
The Thread constructor you are calling takes a reference to a Runnable object as an argument and when the start() method is called on the Thread object the Runnable object's run() method is executed. In this case the object that creates the Thread object is also the Runnable object and hence you pass 'this' to the Thread's constructor.

Having said that, example one is really bad design because you should never pass 'this' to another object from a constructor. The reason is because until the constructor (and any calling constructors) having completed the object isn't fully initialised and so might not be in a consistent state.
Shiv Tattva
Greenhorn

Joined: May 07, 2014
Posts: 6
Thank you Tony Docherty for your nice explanation 😊
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2249
    
  47
My pleasure.
 
 
subject: Confusion in threading