This questions seems common, but I am facing issues while multi-threads are writing into the same file(Excel). Here is my code:
public class XLWriter {
private XLWriter() {
}
private static class SingletonHelper {
private static final XLWriter INSTANCE = new XLWriter();
}
public static synchronized XLWriter getInstance() {
return SingletonHelper.INSTANCE;
}
public static synchronized void writeOutput(Map<String, String> d) {
try {
--- Write file
} catch (Exception e) {
SOP("Not able to write output to the output file.");
}
}
public static void createWorkBook(String fileName, String sheetName)
throws IOException {
try {
-- Create workbook
} catch (WriteException e) {
System.out.println("Could not create workbook" + e);
}
}
I am using testng framework and 10 threads try to write into the same file. Many of the threads are not able to write into it and goes inside the Exception block... What is the better way to do this? Any code samples will greatly help me as I have very less time to finish this.. Thank you.