質問

Wondering about the following scenario (simplified Java code):

//Thread A:

FileOutputStream fos = new FileOutputStream(PATH);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter buffW = new BufferedWriter(osw, SIZE);

for (int i = 0; i < ITER; i++) {
    buffW.write(STR);
}

//Thread B:

fos.getFD().sync();

Is there a need to synchronize the sync() call w/ the other write, or does this get taken care of at OS-level? My gut feeling says that this should be the OS's concern, but I could be wrong.

The docs do not touch on this subject, I get only a hint that there's some synchronization going on for FileChannel.force(...), which should be equivalent, but the scenario in the docs seems somewhat different.

役に立ちましたか?

解決

It should be thread safe, since file descriptors are managed by the OS. It seems like a rather strange thing to want to do, though.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top