Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

run() and start() question

 
Zhao zhenhua
Ranch Hand
Posts: 37
Firefox Browser IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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



Thanks

Anil Kumar
 
Zhao zhenhua
Ranch Hand
Posts: 37
Firefox Browser IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Chandra and anil .I understand now.The answer is 3.
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Zhao zhenhua",

Please Quote Your Sources.

Thanks,
Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic