This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am confused with the output of this code. This is from Dan's Mock Exam.Please explain this code and output.








Thanks in advance,
Mubeen.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
T1T1T3

Which bit don't you understand?
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the output confused you? What did you think it would be?
 
Mubeen Shaik
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

new Thread(new A5(),"T2").run();

I thought it will be "T1T2T3". I think my Threads concept is not clear.



Thanks,
Mubeen.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the difference between calling the run() method and calling the start() method. It's a little tricky since you need to implement a run() method.

To start a thread running you have to call the start() method. Calling run() just executes in the same thread.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call to Thread.run() merely delegates to it's encapsulated Runnable.run(). No new thread is started - it all executes in the same thread.

Calling Thread.start() is a direction to the thread scheduler to start a new thread. The thread scheduler, at its own discretion, will execute by calling the Thread.run() method - this occurs in the new thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic