<div dir="ltr">Hello,<div><br></div><div>Using JDK-20, I have this code:</div><div><br></div><div><div style="color:rgb(204,204,204);background-color:rgb(31,31,31);font-family:Menlo,Monaco,"Courier New",monospace;font-size:12px;line-height:18px;white-space:pre"><div>@<span style="color:rgb(78,201,176)">Test</span></div><div>    <span style="color:rgb(86,156,214)">public</span> <span style="color:rgb(78,201,176)">void</span> <span style="color:rgb(220,220,170)">testRR</span>() <span style="color:rgb(86,156,214)">throws</span> <span style="color:rgb(78,201,176)">Exception</span> {</div><div>        <span style="color:rgb(78,201,176)">var</span> <span style="color:rgb(156,220,254)">x1</span> <span style="color:rgb(212,212,212)">=</span> <span style="color:rgb(220,220,170)">openForReading</span>(<span style="color:rgb(156,220,254)">file</span>);</div><div>        <span style="color:rgb(197,134,192)">try</span> {</div><div>        <span style="color:rgb(78,201,176)">var</span> <span style="color:rgb(156,220,254)">x2</span> <span style="color:rgb(212,212,212)">=</span> <span style="color:rgb(220,220,170)">openForReading</span>(<span style="color:rgb(156,220,254)">file</span>);</div><div>        <span style="color:rgb(220,220,170)">close</span>(<span style="color:rgb(156,220,254)">x2</span>);</div><div>        } <span style="color:rgb(197,134,192)">finally</span> {</div><div>            <span style="color:rgb(220,220,170)">close</span>(<span style="color:rgb(156,220,254)">x1</span>);</div><div>        }</div><div>    }</div><div><br></div><div><div style="line-height:18px"><div><span style="color:rgb(86,156,214)">private</span> <span style="color:rgb(78,201,176)">RafChannelPair</span> <span style="color:rgb(220,220,170)">openForReading</span>(<span style="color:rgb(78,201,176)">String</span> <span style="color:rgb(156,220,254)">filename</span>) <span style="color:rgb(86,156,214)">throws</span> <span style="color:rgb(78,201,176)">FileNotFoundException</span>, <span style="color:rgb(78,201,176)">IOException</span> {        </div><div><span style="color:rgb(78,201,176)">RandomAccessFile</span> <span style="color:rgb(156,220,254)">raf</span> <span style="color:rgb(212,212,212)">=</span> <span style="color:rgb(197,134,192)">new</span> <span style="color:rgb(220,220,170)">RandomAccessFile</span>(<span style="color:rgb(156,220,254)">filename</span>, <span style="color:rgb(206,145,120)">"r"</span>);</div><div>        <span style="color:rgb(78,201,176)">FileChannel</span> <span style="color:rgb(156,220,254)">channel</span> <span style="color:rgb(212,212,212)">=</span> <span style="color:rgb(156,220,254)">raf</span>.<span style="color:rgb(220,220,170)">getChannel</span>();</div><div>        <span style="color:rgb(78,201,176)">var</span> <span style="color:rgb(156,220,254)">lock</span> <span style="color:rgb(212,212,212)">=</span> <span style="color:rgb(156,220,254)">channel</span>.<span style="color:rgb(220,220,170)">tryLock</span>(<span style="color:rgb(181,206,168)">0</span>, <span style="color:rgb(78,201,176)">Long</span>.<span style="color:rgb(79,193,255)">MAX_VALUE</span>, <span style="color:rgb(86,156,214)">true</span>);</div><div>        <span style="color:rgb(197,134,192)">if</span> (<span style="color:rgb(156,220,254)">lock</span> <span style="color:rgb(212,212,212)">!=</span> <span style="color:rgb(86,156,214)">null</span> <span style="color:rgb(212,212,212)">&&</span> <span style="color:rgb(212,212,212)">!</span><span style="color:rgb(156,220,254)">lock</span>.<span style="color:rgb(220,220,170)">isShared</span>()) {</div><div>            <span style="color:rgb(78,201,176)">System</span>.<span style="color:rgb(79,193,255)">out</span>.<span style="color:rgb(220,220,170)">println</span>(<span style="color:rgb(206,145,120)">"Unable to acquire shared lock"</span>);</div><div>        }</div><div>        <span style="color:rgb(197,134,192)">return</span> <span style="color:rgb(197,134,192)">new</span> <span style="color:rgb(220,220,170)">RafChannelPair</span>(<span style="color:rgb(156,220,254)">raf</span>, <span style="color:rgb(156,220,254)">channel</span>, <span style="color:rgb(156,220,254)">lock</span>);</div><div>    }</div><br><div>    <span style="color:rgb(86,156,214)">private</span> <span style="color:rgb(78,201,176)">void</span> <span style="color:rgb(220,220,170)">close</span>(<span style="color:rgb(78,201,176)">RafChannelPair</span> <span style="color:rgb(156,220,254)">x</span>) <span style="color:rgb(86,156,214)">throws</span> <span style="color:rgb(78,201,176)">IOException</span> {</div><div>        <span style="color:rgb(197,134,192)">if</span> (<span style="color:rgb(156,220,254)">x</span>.<span style="color:rgb(79,193,255)">lock</span> <span style="color:rgb(212,212,212)">!=</span> <span style="color:rgb(86,156,214)">null</span>) {</div><div>            <span style="color:rgb(156,220,254)">x</span>.<span style="color:rgb(79,193,255)">lock</span>.<span style="color:rgb(220,220,170)">release</span>();</div><div>        }</div><div>        <span style="color:rgb(156,220,254)">x</span>.<span style="color:rgb(79,193,255)">channel</span>.<span style="color:rgb(220,220,170)">close</span>();</div><div>        <span style="color:rgb(156,220,254)">x</span>.<span style="color:rgb(79,193,255)">raf</span>.<span style="color:rgb(220,220,170)">close</span>();</div><div>    }</div><div><br></div><div>I expect the code to throw no exception but when I run it I get:</div><div><br></div><div>[ERROR] testRR(com.mycompany.app.FileLockingTest)  Time elapsed: 0.007 s  <<< ERROR!<br>java.nio.channels.OverlappingFileLockException<br> at com.mycompany.app.FileLockingTest.openForReading(FileLockingTest.java:69)<br>  at com.mycompany.app.FileLockingTest.testRR(FileLockingTest.java:52)<br></div><div><br></div><div>is this not a bug? can someone explain it to me? if this is as expected then what difference does it make whether one gives true or false to the shared parameter of <span style="color:rgb(53,56,51);font-size:1.3em">public abstract </span><a href="https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/java/nio/channels/FileLock.html" title="class in java.nio.channels" style="font-size:1.3em;text-decoration-line:none;color:rgb(76,107,135)">FileLock</a><span style="color:rgb(53,56,51);font-size:1.3em"> tryLock(long position,</span></div><pre style="font-size:1.3em;color:rgb(53,56,51)">               long size,
               boolean shared)</pre><pre style="font-size:1.3em;color:rgb(53,56,51)">Thanks,</pre><pre style="font-size:1.3em;color:rgb(53,56,51)">M.</pre></div></div></div></div></div>