aspose file tools*
The moose likes Threads and Synchronization and the fly likes Please Explain The Output. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Please Explain The Output." Watch "Please Explain The Output." New topic
Author

Please Explain The Output.

Veena Nair
Greenhorn

Joined: Jul 08, 2012
Posts: 13
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
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Veena Nair
Greenhorn

Joined: Jul 08, 2012
Posts: 13
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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. ;)

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Veena Nair
Greenhorn

Joined: Jul 08, 2012
Posts: 13
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

Joined: Apr 06, 2010
Posts: 4363
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please Explain The Output.