This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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: 1509
    
    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: 42028
    
  64
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 ??