Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why should we pass the object of the class implemented from runnable to thread class constructor?

 
shankara me
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi my doubt is in java.My doubt is on creating thread by implementing runnable interface.
Please see the below programme.
In below programme I have implemented the class countdowneven from Runnable.
In main method I am passing the object of countdown even to constructor of thread class.
Why should we pass the object of countdowneven to constructor of thread class?
What operations will be performed by Thread class constructor on object of countdowneven?
-------------------------------------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------------------------------------------
Output:
10
8
6
4
2

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you implement the Runnable interface, the class becomes a Runnable object. There is a constructor of the Thread class which accepts a Runnable parameter. When the start method is called on that thread, it will cause the run() method in the Runnable class to execute.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic