Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Thread and method run

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt about the output this code.

The source is iRix Technologies .Practice Test 1.



Output of 1 to 100 followed by "end".

I think that it would print just "end" because the method start() in the class Thread would call the method run(). As the method run() is not overriden then it would print nothing so the output would be just "end".

Could anyone help me?
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are actually overriding the start method of the Thread class. By doing so what you are actually doing is not calling the run from it so you are not actually making a new stack but its a simple method call to the start().


The Thread used always is Main here. Change start() method implementation to run() and then the thread will run is a different stack.

Actually start() of Thread class materializes a new stack and then calls run from it so that whatever is there in run to use that stack. But you defined the start method but you din't provide any mechanism to make a new stack so its a simple method call. Well the run() method comes from the Runnable interface which is implemented by the thread class itself.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well its more or less same to toString method of the Object class. If you over ride it in your class then the class will use the overridden method. Its a simple concept and Thread class is no different than this.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic