Parallel file access on Android

I know that many OSs do some file system locking to prevent conflicting views. Are there any guarantees that Java and / or Android ensure the security of file access streams? I would like to learn as much as possible about this before I start writing concurrency code.

If I missed a similar question that was answered, do not hesitate to close this topic. Thanks.

+7
source share
2 answers

Android is built on top of Linux, so it inherits the semantics of the Linux file system. Unless you explicitly lock the file, several applications and streams may open it for read / write access. If you really don't need synchronization between processes, I would suggest using the usual Java synchronization primitives for arbitrarily accessing a file.

+9
source

The normal read / write function (FileInputStream, etc.) does not provide AFAIK to ensure thread safety. To ensure thread safety, you need to use the FileChannel . It will look something like this:

FileInputStream in = new FileInputStream("file.txt"); FileChannel channel = in.getChannel(); FileLock lock = channel.lock(); // do some reading lock.release(); 

I would read File Lock doc and take care of the stream!

+5
source

All Articles