views:

109

answers:

2

Using java IO, it seems like forking a new process gives better ability for a process B to read data written by process A to file than what you could get if thread A wrote to a file that thread B is trying to read (within the same process).

It seems like the rules are not comparable to the memory model. So what file-based concurrency works ? References would be appreciated.

+4  A: 

Any observations like this bound to be operating system specific, and may be specific to different versions of the operating system (kernel). What you are hitting here is probably related to the way that the OS implements threads, and thread scheduling. The Java platform provides little in the way of tuning for this kind of thing.

IMO, if you need better performance, you probably should not be using a file as a data transfer channel between two threads in the same JVM. Code your application to detect that the threads are colocated in the same JVM and use (say) Java Pipe streams.

Stephen C
A: 

Maybe it could have to do with thread and process blocking.

When a process wants a resource (writing/reading a file) it blocks untils the S.O. fulfills the requirement and return something to the process.

If you are not using hyperthreading a process with two threads will block both threads for fullfilling each one of the tasks. But if you separate them, maybe the S.O. can optimize access and paralelize the read/write better.

(just guessing :)

helios