Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Multi Threads writing into text file

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a requirement where the same text file is to be used and written by multiple threads. I use BufferedWriter (I use a static variable to know whether the file is open or close, so with this only the first thread has to open and write the file, others have to just write into the file) , so just a synchronization on the BufferedWriter object will suffice ?.

writeMethod (BufferedWriter out)
{

synchronized(this) {
out.write("testing");
}

}

Will this alone make my code safe ?. Thanks in advance.

Regards,
Saravanan.V.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A synchronization on the BufferedWriter object would work, but that's not what the code you posted is doing. Whether it's thread-safe depends on what "this" is - is it guaranteed that only a single instance of the class that contains the code will exist? If not, then it's not thread-safe.
 
Saravanan Venkat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot for the reply. Yes, with in the thread there will only one instance of the class where the method resides. I hope this will make the code thread-safe.

Regards,
Saravanan.V.
 
Marshal
Posts: 27211
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me that synchronizing on the BufferedWriter would work (if there's only one of them, as there should be). So why don't you just do that?
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point is your saying your synchronizing on 'out' but your code snipper synchronizes on this so if this == out for instance your ok, or you always synchronize on the same this where ever you use out.

Your post is a bit confusing as to what you intend.
 
Saravanan Venkat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Yes, I am synchronizing 'this' where ever I use out.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, so what has fixed it finally? Am looking for a solution to the same scenario. Posting the solution here would be of great help.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Wilson wrote:Hi, so what has fixed it finally? Am looking for a solution to the same scenario. Posting the solution here would be of great help.



The complete answer is already in this thread. And why would you expect the original poster to still be monitoring this thread a year and a half after his problem was solved?

If you're still having problems, please post details about what you tried and where you're stuck. And remember, this site is NotACodeMill.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Wilson wrote:Hi, so what has fixed it finally? Am looking for a solution to the same scenario. Posting the solution here would be of great help.



as Jeff has said this site is NotACodeMill so i will not give the complete code, this is just to help you(Sandra)
Below class 'Data' is based on singleton pattern and write to file is synchronised in writeToFile() method on the instance of Data class, here there is no need to synchronize on the PrintWriter Object
Now the runnable code for the multiple threads execution,


Now create multiple threads through Pool or directly and check the execution.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic