I wrote the code given below, but did not get the expected Output.
Output is: Hello Thread...!
Im printed thru Thread1...!!
Expected Output is: Im printed thru Thread1...!!
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.
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.
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 ??
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com