File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Non syncronized output even when accessing syncronized method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Non syncronized output even when accessing syncronized method" Watch "Non syncronized output even when accessing syncronized method" New topic
Author

Non syncronized output even when accessing syncronized method

Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

Hi .. Please consider following example.. where i am reading two files from c:/FileIO folder
This is a class that reads a text file ..




This is the main class


Note : I am reading 2 files from c:/FileIO

Now when i run the MainClass i get an output that is a mingled output from two files...

However on creating a new file ReadingFile, and defining readFile method in it... I get the proper output.. Suddenly
synchronized keyword starts working.. The MainClass remains as it is







Can you please explain the phenomenom behind this..
Edward Harned
Ranch Hand

Joined: Sep 19, 2005
Posts: 291

Can you please explain the phenomenon behind this..


Can you please read this: http://sscce.org/


Ed's latest article: A Java Parallel Calamity http://coopsoft.com/ar/Calamity2Article.html
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
In your main() method, you create one ReadingFile object, and two different FileRead objects. If readFile() is a synchronized method defined in ReadingFile, then every time that method is called, it's synchronized on the same single instance of ReadingFile, and synchronization prevents two method calls from occurring concurrently on the same instance. That gives you the output you expect, apparently.

However if readFile() is a synchronized method defined in FileRead, then when that method is called using two different FileRead objects, synchronization has no effect. This is normal for synchronized instance methods - they only affect concurrent calls using the same instance. They have no effect on calls using different instances.

Note that if readFile() were a static synchronized method in ReadFile, then synchronization would prevent multiple concurrent invocations of the method, period. Static synchronized methods exclude execution of other static synchronized methods in the same class, regardless of what instances may have been involved. That's how they were designed. But if the method is not static, then synchronization is tied to the specific instances involved.

Note - I'm not suggesting you should rewrite readFile() to be static, as that's probably too much work for this example. I just offer this information for comparison. Synchronized instance methods and synchronized static methods behave differently, and that's a difference you need to pay attention to.
Abhineet Kapil
Ranch Hand

Joined: Feb 08, 2010
Posts: 52

Mike , thanku so much..... doubts cleared.. code running properly........
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Non syncronized output even when accessing syncronized method