Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread creation ambiguity  RSS feed

 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to start of with a little code as follows in general:


So i replaced line below:

with


The code runs fine and i get the same output without the above line. I want to know what is the basic difference between both the statements? Is it just that I m creating an object "t" of type NewThread or since "NewThread" simply" implements Runnable" or "extends Thread" so that the statement runs implicitly and thats why we only include statement as there is no need to create object?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dhananjay Deshmukh wrote:
The code runs fine and i get the same output without the above line. I want to know what is the basic difference between both the statements? Is it just that I m creating an object "t" of type NewThread or since "NewThread" simply" implements Runnable" or "extends Thread" so that the statement runs implicitly and thats why we only include statement as there is no need to create object?


You are kinda mixing terminology. The first case declares an "i" reference variable which is assigned to the object that is created. And the second does not declare the variable. But in both cases, an object is created (notice the new operator?)... so, in both cases, a thread object (and thread) is created (and started), and the thread runs.

Henry
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's possible that calling your class "NewThread" has led you into believing that it's some kind of thread. Which it isn't; it only contains a Thread. If you rename your class something else -- like "RanchDemo" -- you'll see that it acts exactly the same way. When you create a RanchDemo object, a Thread is created and started. It makes no difference whether that RanchDemo object is subsequently assigned to a reference variable or not.
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It's possible that calling your class "NewThread" has led you into believing that it's some kind of thread. Which it isn't; it only contains a Thread. If you rename your class something else -- like "RanchDemo" -- you'll see that it acts exactly the same way. When you create a RanchDemo object, a Thread is created and started. It makes no difference whether that RanchDemo object is subsequently assigned to a reference variable or not.


that explains why i was getting confused. Because i tried to create a simple program without either implementing runnable or thread and tried to create an object like "new Classname". And ended up with error i couldn't figure out. Well, so much for trying variation however i think it was worth it. Its like the entire thread chapter just became easy to understand.
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It's possible that calling your class "NewThread" has led you into believing that it's some kind of thread. Which it isn't; it only contains a Thread. If you rename your class something else -- like "RanchDemo" -- you'll see that it acts exactly the same way. When you create a RanchDemo object, a Thread is created and started. It makes no difference whether that RanchDemo object is subsequently assigned to a reference variable or not.


One more question that i have for you is that I have observed the statement below



And another statement is used in another class to create object as well as thread that has main and also the fact that NewThread class "implements Runnable" interface. So are these two ways of creating thread?
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Neither of them are ways of creating a thread.

The first one (Thread t = Thread.currentThread) doesn't create a thread. It just gets a reference to a thread which already exists (the thread which is currently running this code) and assigns it to a variable.

And the second one (new NewThread()) creates an object which -- as already mentioned -- isn't a thread. It happens that the constructor of that object creates a new thread, but as I already pointed out it's easy to confuse that thread with the NewThread object when they are actually two different things.

And the fact that a class implements Runnable is nothing to do with the creation of threads, either. All that means is that the class must have a "public void run()" method, and that's all.
 
Dhananjay Deshmukh
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:No. Neither of them are ways of creating a thread.

The first one (Thread t = Thread.currentThread) doesn't create a thread. It just gets a reference to a thread which already exists (the thread which is currently running this code) and assigns it to a variable.

And the second one (new NewThread()) creates an object which -- as already mentioned -- isn't a thread. It happens that the constructor of that object creates a new thread, but as I already pointed out it's easy to confuse that thread with the NewThread object when they are actually two different things.

And the fact that a class implements Runnable is nothing to do with the creation of threads, either. All that means is that the class must have a "public void run()" method, and that's all.



Ok now i m clear.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!