Link to home
Start Free TrialLog in
Avatar of lwinkenb
lwinkenb

asked on

Thread question

I have multiple threads that are reading and writing to text files.  How can I be sure that a Thread wont try to access the text file while while another is reading or writing to the same one?

I can't use the Synchronize command on the method, because I want the method to be able to handle multiple threads writing to different files at the same time.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

What's wrong with:

public synchronized void writeToFile(String fileName){}

?
Avatar of allahabad
allahabad

When one thread is writing it locks the resource, so you would need to wory that any other thread will go and write to the same file, when one thread is locking.

public synchronized void writeToFile(String fileName) , this is synchronized which  means for other thread  to get access to File object, lock need to be released.

No need to write Synchronize on the method, call  writeToFile, this will lock the file resource when one thread is writing, and release when File object is closed or exception is thrown.
If you insist on not using the synchronized (not even in a part of the method) you can (maybe) use a HashMap in order to keep track on all the files you are currently writing to or reading from. You will have to update this HashMap all the time.
--
Or even

public synchronized void accessFile(String fileName, boolean reading) {
  // if not reading then writing
}

?
Avatar of lwinkenb

ASKER

CEHJ >>
If I did it that way, my understanding is if one thread passed through "fileA.txt", and a second thread passed through "fileB.txt", the method would wait for the first thread to exit the method before the second thread started.

I want both files to be written at the same time if they are different.  Is my understanding of how the Synchronized method works wrong?  I'm still learning here.

Thanks,
Yes, you can do. because "fileA.txt" and "fileB.txt" are  two different File object.
You'll have to use a technique like shji1 has recommended as using the method i suggested will allow only one operation at once so it will limit your potential writes
ASKER CERTIFIED SOLUTION
Avatar of warsql
warsql

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
FileNotFoundException should be thrown if file is in use.  Then we'll wait at least .1 seconds to try again.  If we hit a serious problem, we'll never be able to write to the file.  Something similar could be done for reading.
The solution is to synchronize on an object that is unique to the particular file that you are writing to.  That way you will block other writers to the same file, but not block other writers to different files.

You just need to be sure that the objects you are synchronizing on have a one-to-one correspondence with the files.

For example:

File file1 = new File("file1.txt");
File file2 = new File("file2.txt");
FileWriter fw1 = new FileWriter( file1 );
FileWriter fw2 = new FileWriter( file2 );

now you can launch lots of threads that write to these files as long as they write using calls to a method that synchronizes on the appropriate FileWriter:

void write( FileWriter fw, String text ) throws IOException {
  synchronized(fw) {
    fw.write( text );
  }
}

So writes to fw1 will synchronize on fw1 (blocking other writers to fw1, but not writers to fw2).  Likewise for fw2.
Thanks to everyone for the help.  I accepted warsql's answer because his was the closest to what I implemented.