The following is a snippet outlining the purpose of this unit test. The figure below shows that it createFileperforms only one task, which is already known as a thread-safe operation.
Thus, the idea is more connected with the test than with the real work; to prove beyond the shadow of doubt, the behavior of a thread-protected method is that it behaves as we have already proven historically.
public static final synchronized void createFile(final File file) throws IOException {
file.createNewFile();
}
@Test
public void testCreateFileThreadSafety() throws Exception {
for (int i = 1; i < 50; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
createFile(new File(i+".txt"));
new File(i+".txt").delete();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}).start();
assertTrue(file.getParentFile().listFiles().length == 0);
}
}
EDIT:
What happens now: a stream is created, a file is created, the file is deleted, the threads die, they say nothing is proved, and they repeat
Expected: threads must begin and be approved to ensure that only one file is created at a time and that other threads are waiting and not executing a method
DOUBLE EDITING:
unit test, , ( )