This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Mubeen Shaik
Ranch Hand
Posts: 67
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic