wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes run() and start() question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "run() and start() question" Watch "run() and start() question" New topic
Author

run() and start() question

Zhao zhenhua
Ranch Hand

Joined: Jul 17, 2006
Posts: 37

Hi,every ranchers!


The correct answer is 4,why?My chosen is 2,i think that the order cannot be determined. Who can help me explain why?


Head first Servlet and JSP
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Zhao,

See the flow of code, very first the lb.run() will be called because it comes
first in the code. After that you pass the lb to the Thread constructor and
call the start method that thread will run in its own call stack. The
invocation of run() is from the main() thread.

Inside the run method you write the following line:



You will get output like:
main (This is main thread)
Thread 0 (a new thread you created inside the main thread)


Note: Order wont be determined if both the times you did like
new Thread(lb).start(), because you didn't know which run() will be
granted to execute first.

But in your case, first is simple method call followed by thread creation
and making it runnable by calling start() method.


Thanks,


cmbhatt
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

i think the answer is 3 .run1 followed by run2.



Thanks

Anil Kumar
Zhao zhenhua
Ranch Hand

Joined: Jul 17, 2006
Posts: 37

Thank you Chandra and anil .I understand now.The answer is 3.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18990
    
  40

"Zhao zhenhua",

Please Quote Your Sources.

Thanks,
Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: run() and start() question