File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non syncronized output even when accessing syncronized method

 
Abhineet Kapil
Ranch Hand
Posts: 52
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 291
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please explain the phenomenon behind this..


Can you please read this: http://sscce.org/
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike , thanku so much..... doubts cleared.. code running properly........
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic