How to prevent the other threads from accessing a method when one thread is accessing a method?
- by geeta
I want to search for a string in 10 files and write the matching lines to a single file. I wrote the matching lines from each file to 10 output files(o/p file1,o/p file2...) and then copied those to a single file using 10 threads.
But the output single file has mixed output(one line from o/p file1,another line from o/p file 2 etc...) because its accessed simultaneously by many threads. If I wait for all threads to complete and then write the single file it will be much slower.
I want the output file to be written by one thread at a time. What should i do?
My source code:(only writing to single file method)
public void WriteSingle(File output_file,File final_output) throws IOException {
synchronized(output_file){
System.out.println("Writing Single file");
FileOutputStream fo = new FileOutputStream(final_output,true);
FileChannel fi = fo.getChannel();
FileInputStream fs = new FileInputStream(output_file);
FileChannel fc = fs.getChannel();
int maxCount = (64 * 1024 * 1024) - (32 * 1024);
long size = fc.size();
long position = 0;
while (position < size) {
position += fc.transferTo(position, maxCount, fi);
}
}
}