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 Multi Threads writing into text file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Multi Threads writing into text file" Watch "Multi Threads writing into text file" New topic
Author

Multi Threads writing into text file

Saravanan Venkat
Greenhorn

Joined: Apr 17, 2010
Posts: 5
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
Marshal

Joined: Mar 22, 2005
Posts: 32767
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.


Android appsImageJ pluginsJava web charts
Saravanan Venkat
Greenhorn

Joined: Apr 17, 2010
Posts: 5
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
Bartender

Joined: Oct 14, 2005
Posts: 13842

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

Joined: Oct 26, 2003
Posts: 343

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.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Saravanan Venkat
Greenhorn

Joined: Apr 17, 2010
Posts: 5
Thanks for the reply. Yes, I am synchronizing 'this' where ever I use out.
Sandra Wilson
Greenhorn

Joined: Nov 17, 2011
Posts: 1
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

Joined: Jan 03, 2004
Posts: 3133
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

Joined: Oct 19, 2011
Posts: 54
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.
 
 
subject: Multi Threads writing into text file
 
Threads others viewed
insertion and deletion of record from a text file
Writing to a text file.
problem writing to text file
How to replace data in a file
Want to append some text in the existing file
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture