and after certain modifications i executed the program.
Program is printing 5 times Consistent with thread id 8 and 5 times Consistent with thread id 9 that means thread starting first is finishing first thread starting last is finishing last even when i have removed synchronized block
I am not getting why program is giving output in a synchronized pattern even when synchronized block is not present...
The real life behavior of thread execution depends upon thread manager. Since the time required to print 5 numbers is very less, thread manager might be deciding to execute one thread after another (or it may appear that this is happening). However, this behavior is not guaranteed.
E.g. try inserting Thread.sleep(<some_time>) inside for loop. And check the results with and without synchronized block.
The real life behavior of thread execution depends upon thread manager. Since the time required to print 5 numbers is very less, thread manager might be deciding to execute one thread after another (or it may appear that this is happening).
That's an important point. Just because two instructions (or even threads) *can* be executed in parallel, doesn't mean that they will. There's an overhead in synchronizing threads, and switching between them, so that the JVM may well decide not to execute them in parallel, or at least not until the first thread has executed for at least X milliseconds.
i ran the code and as far as i concerned starting thread without transitioning could give unpredictable output :P
Joined: Jul 12, 2010
OK I have understood that and i have also executed the program by increasing the number from 5 to 100 now program is giving normal output...
Thanks to both of you for clearing my doubt and thanks to Edgar for giving me the program
subject: Why this program's output pattern is synchronized ??