I am trying to use ExecutorService in order to write to a file. However, when I execute this code, the execution goes to outFile.close(); before the execution is completed, which throws
java.io.IOException: Stream closed How can I call outFile.close() only after all the task has been completed?
(PS: I have removed all try/catch blocks for code clarity.)
ExecutorService executor = Executors.newFixedThreadPool(3); for (int i = 1; i <= 1000; i++) { final int counter = i; executor.execute(new Runnable() { @Override public void run() { outFile.write(wld.getWord(counter) + "successful"); outFile.write("\n"); } }); } outFile.close();