File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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: 1710
Hi Zhao,

See the flow of code, very first the 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.


anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447

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


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

Joined: Sep 28, 2004
Posts: 20531

"Zhao zhenhua",

Please Quote Your Sources.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: run() and start() question
jQuery in Action, 3rd edition