write access to files by multiple thread

hi when multiple threads write to the same file, do i need to synchrnoize access to the file?If i am writing 30 char strings to the file from each thread and i dont synchrozie can my strings get interleaved ?
[229 byte] By [chhasida] at [2007-11-27 2:44:24]
# 1
interleave might happen only when two or more threads accessing the common data. If the writing content is common, you should synchronize.
AnanSmritia at 2007-7-12 3:10:58 > top of Java-index,Core,Core APIs...
# 2
but FileOutputStream's write(byte[] b) is not synchronized.so if 2 threads call this method at the same time with differentbyte arrays, cant they get interleaved ?
chhasida at 2007-7-12 3:10:58 > top of Java-index,Core,Core APIs...
# 3
Yes. Isn't that what he just said? Is somebody disagreeing?Although ... at the OS level writes are atomic. But you don't have a guarantee of how java writes map on to OS writes so this doesn't really help you.
ejpa at 2007-7-12 3:10:58 > top of Java-index,Core,Core APIs...
# 4
But upto what i know.. the OS writes are atomic in all cases. So writing doesnt actually require synchronise block if its writing directly to the file.
unaveenkumara at 2007-7-12 3:10:59 > top of Java-index,Core,Core APIs...
# 5
but you don't have a guarantee that a single Java write maps into a single OS write .. see above
ejpa at 2007-7-12 3:10:59 > top of Java-index,Core,Core APIs...
# 6
> Although ... at the OS level writes are atomic. On some platforms this might be true only up to the file-system's buffer size.
BIJ001a at 2007-7-12 3:10:59 > top of Java-index,Core,Core APIs...
# 7

well...sorry if this seems dumb, but i am confused

so i thought i would clarify with a code sample

public class FileTester {

private static final int thread_count = 50;

public static void main(String[] args) {

try {

FileOutputStream fos = new FileOutputStream("out.txt");

Thread t[] = new Thread[thread_count];

for (int i = 0; i < t.length; i++) {

new Thread(new ConcurrentFileWriter(fos)).start();

}

fos.close();

}catch (IOException e) {

e.printStackTrace();

}

}

}

class ConcurrentFileWriter implements Runnable{

int times = 3;

FileOutputStream fos;

public void run() {

for (int i = 0; i < times; i++) {

try {

fos.write(new String("thread id :" + Thread.currentThread().getId() + " - " + i + "\n").getBytes());

} catch (IOException e) {

e.printStackTrace();

}

}

}

public ConcurrentFileWriter(FileOutputStream fos) {

this.fos = fos;

}

}

so from this example the writing content is different(to clarify my position in response to anans reply).

but all threads are writing to the same fileoutputstream.

now in my app its ok if i get the following output - (scenario 1)

thread id: 1 - 1

thread id: 2 - 1

thread id: 1 - 2

and so on.....

is it possible for me to get the following output (strings interleaved) - (scenario 2)

thread id: 1 thread id: 2 - 1

- 1

thread id: 1 -2

I can live with scenario 1, but not with 2.

so is it possible for me to get scenario 2 without synchronization ?

i hope i have clarified what i meant

Message was edited by:

chhasid

Message was edited by:

chhasid

chhasida at 2007-7-12 3:10:59 > top of Java-index,Core,Core APIs...
# 8

I am no language lawyer but I have the feeling this is not exactly defined. I would synchronize the writing on the output stream object to be sure.

I made a test out of curiosity: several threads were writing 2 MB data concurrently on the same output stream object. On Windows the data were written sequentially whereas on Linux the last one overwrrote the previous ones.

BIJ001a at 2007-7-12 3:10:59 > top of Java-index,Core,Core APIs...