wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads 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 "Threads" Watch "Threads" New topic
Author

Threads

Mubeen Shaik
Ranch Hand

Joined: Jan 26, 2004
Posts: 67
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.


Sun Certified Java Programmer
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
T1T1T3

Which bit don't you understand?


Tony Morris
Java Q&A (FAQ, Trivia)
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
What about the output confused you? What did you think it would be?
Mubeen Shaik
Ranch Hand

Joined: Jan 26, 2004
Posts: 67
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

Joined: Oct 10, 2000
Posts: 1364
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

Joined: Sep 24, 2003
Posts: 1608
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.
 
Consider Paul's rocket mass heater.
 
subject: Threads