• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Explain The Output.

 
Veena Nair
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I wrote the code given below, but did not get the expected Output.



Output is:
Hello Thread...!
Im printed thru Thread1...!!
Hello Thread...!
main


Expected Output is:
Im printed thru Thread1...!!
Hello Thread...!
Hello Thread...!
main

Thank You.
 
Henry Wong
author
Marshal
Pie
Posts: 21419
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Veena Nair wrote:Hi,

I wrote the code given below, but did not get the expected Output.



Output is:
Hello Thread...!
Im printed thru Thread1...!!
Hello Thread...!
main


Expected Output is:
Im printed thru Thread1...!!
Hello Thread...!
Hello Thread...!
main


For all modern JVM implementations, the thread ordering is dependent on the underlying scheduler. So, with no mechanism (to enforce the order), what you are seeing *and* what you expect, are both valid outputs. If you need to have a required order, you will need some sort of flag / synchronization / wait-notify setup to enforce the order.

BTW, why did you expect that particular order?

Henry
 
Veena Nair
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for your reply.

I expect the particular output because the run() method must be called once the start() is executed.
I mean the control should be transferred immediately to the run method. Also naming of the thread is done after starting the thread, then how come the thread gets the name "Hello Thread...!" .?
I am at a complete loss.

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Veena Nair wrote:
I expect the particular output because the run() method must be called once the start() is executed.
I mean the control should be transferred immediately to the run method.

The _main control_ is not transferred but 'thread.start()' created another 'control' of execution.
Veena Nair wrote:Also naming of the thread is done after starting the thread, then how come the thread gets the name "Hello Thread...!" .?

Well, the main thread is fast enough to update the thread name before the names getter is called on another thread. If you want to delay that name setting, you can introdce Thread.sleep like this.

Veena Nair wrote:I am at a complete loss.
Well, not here at coderanch. ;)
 
Veena Nair
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sagar, for the valuable information.

I got cleared with what you said. But then the example which you explained gave me a new doubt.
If I call sleep() on thread mt also, I get the same output. How does that happen?? Calling sleep() on main thread and thread mt are the same ??
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Veena Nair wrote:If I call sleep() on thread mt also, I get the same output. How does that happen?? Calling sleep() on main thread and thread mt are the same ??


Do you mean calling mt.sleep(2000)? sleep() is a static method, and it only ever pauses the current thread. You can, like any other static method, call it on a Thread reference, but it's better practice to call it as Thread.sleep(). So, effectively, a thread can pause itself, but not another thread.

So if you want to see different behaviour, try adding a sleep() call inside the run() method of MyThread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic