• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multi Threads writing into text file

 
Saravanan Venkat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Paul Clapham
Sheriff
Posts: 21875
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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?
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks for the reply. Yes, I am synchronizing 'this' where ever I use out.
 
Sandra Wilson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Nomaan Butt
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!