• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Non syncronized output even when accessing syncronized method

 
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 291
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can you please explain the phenomenon behind this..



Can you please read this: http://sscce.org/
 
Master Rancher
Posts: 4225
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike , thanku so much..... doubts cleared.. code running properly........
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic