Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I seek a file from different threads independently using FileChannel?
    text
    copied!<p>I created a web application that works on FLV files.</p> <p>This application uses a library that I created for parsing content from flv files. This library uses FileChannel to seek a file.</p> <p>I'm experiencing a strange behavior now that I seek the same flv file from different threads. Let's say that <strong>Thread_1</strong> and <strong>Thread_2</strong> are both seeking <strong>movie.flv</strong> concurrently (my question comes after the example).</p> <p><strong>Thread_1</strong></p> <pre><code>// Thread_1 moves to position 200 to read something FileChannel chan1 = new FileInputStream("movie.flv").getFileChannel(); chan1.position(200); </code></pre> <p><strong>Thread_2</strong> (executing just after <strong>Thread_1</strong>)</p> <pre><code>// Thread_2 moves to position 600 to read something else FileChannel chan2 = new FileInputStream("movie.flv").getFileChannel(); chan2.position(600); </code></pre> <p>Finally <strong>Thread_1</strong> does:</p> <pre><code>ByteBuffer bb = ByteBuffer.allocate(40); chan1.read(bb); </code></pre> <p>Is <strong>Thread_1</strong> reading 40 bytes from position 200 or from position 600? More precisely, are <em>chan1</em> and <em>chan2</em> independent (=can seek independently) channels or not?</p> <p>From the <a href="http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html#getChannel%28%29" rel="nofollow">documentation</a> I read that the FileChannel is <strong>unique</strong>, so my bet (unfortunately) is that in the example <strong>Thread_1</strong> is going to read from position 600 :\</p> <p>In case, can you suggest a different approach for seeking a file independently from different threads?</p> <p>thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload