How to prevent the other threads from accessing a method when one thread is accessing a method?

Posted by geeta on Stack Overflow See other posts from Stack Overflow or by geeta
Published on 2010-05-19T14:40:03Z Indexed on 2010/05/19 14:50 UTC
Read the original article Hit count: 196

Filed under:
|

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);
       }
    }
}

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading