File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why this program's output pattern is synchronized ?? 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 "Why this program Watch "Why this program New topic
Author

Why this program's output pattern is synchronized ??

vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
I have copied the program of Edgar Trania
lock question topic starter
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...

Modifications-:
1) Synchronized block removed

2) Thread.currentThread().getId() added

Modified program-:


Edgar Trania'a original code-:

Please clear my doubt.

Thanks
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1506
    
    5

Hi vishal,

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.

I hope this will clear the doubt.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41818
    
  62
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.


Ping & DNS - my free Android networking tools app
Edgar Trania
Greenhorn

Joined: Feb 14, 2013
Posts: 7
i ran the code and as far as i concerned starting thread without transitioning could give unpredictable output :P
vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why this program's output pattern is synchronized ??