このテストを実行すると
public class Test extends Thread {
String str;
Test(String s) {
this.str = s;
}
@Override
public void run() {
try {
FileWriter fw = new FileWriter("1.txt", true);
for (char c : str.toCharArray()) {
System.out.print(c);
fw.write(c);
}
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
new File("1.txt").delete();
new Test("11111111111111111111").start();
new Test("22222222222222222222").start();
}
}
1.txtに文字を書き込む方法を正確に示しています
2222222222222222111211111211111121211111
しかし、1.txtでは別の結果が表示されます
2222222222222222222211111111111111111111
何故ですか?