• Post Reply Bookmark Topic Watch Topic
  • New Topic

nuances of Thread creation  RSS feed

 
Marcus Deviln
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I understand that this throws an exception but the book says that "|work|" prints twice. I'm clueless as to why "|work|" prints twice...


page 625 courtesy of A Programmers Guide to Certification, Mughal & Rasmussen...Addison Wesley copyright 2009
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second call to worker.start() will throw an Exception yes. But the first call to worker.start() causes the run() method to be called in a different thread, printing "|work|" once. You then call worker.run() which will simply execute the same method in the current thread (not a new one), again printing "|work|".
 
Marcus Deviln
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply Rob! That was very informative.
 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
worker.start();
worker.run();
worker.start();


compile time error because of worker.run()
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:
worker.start();
worker.run();
worker.start();


compile time error because of worker.run()


what is the error message you are getting ?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no compiler error. Thread implements Runnable so there is always a run() method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!