• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When the Thread start() method invokes the run method execution order? Can anybody explain me?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I comiple this code



and the output were
==============
first
Hello World!
Thread[Rajansellapan,5,main]

I thought that output must be
first
Thread[Rajansellapan,5,main]
Hello world


Why the Start method called last? meas printing Hello world and then the start method invokes the run() method why?

A similar question I have noticed Deveka corey sir Exam lab, and http://scjptest.com/, can anybody explain me.

The below program also the start method invoke the run() method last, why



---------- Java Execute ----------
first printing
second printing
Hello World!
Thread-0

I thought the output is

first printing
Thread-0
second printing
Hello World!

Dear Ranchers can anybody help me in this regard


 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UseCodeTags when you post coding, use EDIT button to edit and put code tags.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When the Thread start() method invokes the run method execution order? Can anybody explain me?



The start() method doesn't directly call the run() method. The start() method simply creates a new thread, and the newly created thread, will eventually call the run() method, after it does it startup.

As for the order, that completely depends on the thread scheduler, since these methods are being executed by two different threads.

Henry
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's simple,just know one thing that calling the start method on instance of a thread may or may not immediately invokes its run method in seperate call stack.It just moves the thread from new state to runnable state.After that it depends on scheduler to select the execution of a thread.


Even if you call the start method it doesn't gurantee that the thread will be immediately moved into running state.What it gurantees is that the thread will be moved into runnable state and will become eligible to be selected by the scheduler.
 
rajan sellapan
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry Abimaran Kugathasan , I forgot to put the code tags, thanks for your remainder,

thank you Henry Wong, Harshit Sethi

Am satisfied and understand with the answer, suppose in exam the same type of question asked, should I choose the run method output at last.means


---------- Java Execute ----------
first printing
second printing
Hello World!
Thread-0
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In exam you will be asked for the possible answers and those can be more than one.So that makes it a mutiple choice question.
 
You've gotta fight it! Don't give in! Read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic