I have a class that has a main, implements runnable...
Right now my main class will call one of my methods depending on my input
For this one particular method it opens a file and then creates threads calling their run methods via start().
My class has three members that I use
String buffer ="";
static BufferedReader dis;
static PrintWriter out;
In my method that creates the threads, I open a file for reading and open a file for writing.
In my run method, I use dis to read the file and out to write to the file. buffer is what I am reading from the input file, and buffer is also what
I am writing to the output file...
Originally, I was just reading from a file and doing no writing to a file.
Tis seemed to work:
synchronized(buffer){
buffer = dis.readLine();
out.println(buffer);
}
I thought that that kind of synchronization would work... For some reason, I don't think that is the best way...
What should I do so that a
thread is not accessing the input and putput file at the same time?
Also, should I create a local variable in my run method to assign buffer to? I use buffer to perform some other operations later on in my run method...