Domanda

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.

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top