I created a web application that works on FLV files.
This application uses a library that I created for parsing content from flv files. This library uses FileChannel to seek a file.
I’m experiencing a strange behavior now that I seek the same flv file from different threads. Let’s say that Thread_1 and Thread_2 are both seeking movie.flv concurrently (my question comes after the example).
Thread_1
// Thread_1 moves to position 200 to read something
FileChannel chan1 = new FileInputStream("movie.flv").getFileChannel();
chan1.position(200);
Thread_2 (executing just after Thread_1)
// Thread_2 moves to position 600 to read something else
FileChannel chan2 = new FileInputStream("movie.flv").getFileChannel();
chan2.position(600);
Finally Thread_1 does:
ByteBuffer bb = ByteBuffer.allocate(40);
chan1.read(bb);
Is Thread_1 reading 40 bytes from position 200 or from position 600?
More precisely, are chan1 and chan2 independent (=can seek independently) channels or not?
From the documentation I read that the FileChannel is unique, so my bet (unfortunately) is that in the example Thread_1 is going to read from position 600 :\
In case, can you suggest a different approach for seeking a file independently from different threads?
thanks!
I think you’re fine, since you’re creating a new FileInputStream on both Threads. The linked documentation states that the FileChannel returned from FileInputStream.getChannel() is unique to that file input stream.
The documentation of FileChannel also suggests that different FileChannels generated from different sources (e.g. different FileInputStream instances) will not share state.