I’m running into an issue where I have multiple threads that write to the same PrintWriter and not all the data is getting written to the file. I know the multi-threaded part is working correctly since I can print everything to the console. Synchronizing the write statements does not seem to be working. What could be the problem?
ExecutorService pool = Executors.newFixedThreadPool(poolSize);
for (Integer i : map.keySet()) {
final Collection<String[]> set = map.get(i);
pool.submit(new Runnable() {
public void run() {
StringBuffer sb = Matcher.performCollectionMatch(params);
synchronized (this) {
resultFile.print(sb); //this is a PrintWriter - it does NOT capture all sb
resultFile.flush();
System.out.print(sb); //this actually prints out ALL sb
}
}
});
} //FOR loop
Do you close the
PrintWriterafter the pool stops?