Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9152161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:58:32+00:00 2026-06-17T11:58:32+00:00

Specifically, the behaviour I am looking for is this: Read operations happen concurrently, and

  • 0

Specifically, the behaviour I am looking for is this:
Read operations happen concurrently, and will execute once all pending write operations are finished.
Write operations always wait until all other read/write operations are completed.
Close operations always wait until all other read/write operations are completed.
In other words, these operations should be enqueued.

The official documentation for NIO FileLocks does not specify this behaviour. Infact it states that:

File locks are held on behalf of the entire Java virtual machine. They
are not suitable for controlling access to a file by multiple threads
within the same virtual machine.

I have played with idea of manually enqueing all the requests and calling get() on all outstanding Futures before submitting a new I/O request
but I have no idea if this is even a good idea.

How can I achieve this behaviour?

EDIT: Thanks to fge’s insights I have managed to find a basic solution to my problem:

import java.nio.file.Path;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ChannelAccessFactory {

    public static final ExecutorService IO_THREADS = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    private final Path file;
    private final ReadWriteLock lock;

    public ChannelAccessFactory (Path file){
        this.file = file;
        this.lock = new ReentrantReadWriteLock();
    }

    public ReadWriteLock getLock(){
        return lock;
    }

    public ChannelAccess newAccess() throws Exception{
        return new ChannelAccess(file, lock);
    }

}

Wrapped Channel class:

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReadWriteLock;

public class ChannelAccess implements AutoCloseable{

    private final ReadWriteLock lock;
    private final AsynchronousFileChannel channel;

    protected ChannelAccess (Path file, ReadWriteLock lock) throws Exception{
        this.lock = lock;
        this.channel = AsynchronousFileChannel.open(file, StandardOpenOption.READ, StandardOpenOption.WRITE);
    }

    public Future<Integer> read(final ByteBuffer buffer, final long position){
        return ChannelAccessFactory.IO_THREADS.submit(new Callable<Integer>(){
            @Override
            public Integer call() throws InterruptedException, ExecutionException{
                lock.readLock().lock();
                try{
                    return channel.read(buffer, position).get();
                }
                finally {
                    lock.readLock().unlock();
                }
            }
        });
    }

    public Future<Integer> write(final ByteBuffer buffer, final long position){
        return ChannelAccessFactory.IO_THREADS.submit(new Callable<Integer>(){
            @Override
            public Integer call() throws InterruptedException, ExecutionException {
                lock.writeLock().lock();
                try{
                    return channel.write(buffer, position).get();
                }
                finally {
                    lock.writeLock().unlock();
                }
            }
        });
    }

    public long size() throws Exception{
        lock.readLock().lock();
        try{
            return channel.size();
        }
        finally{
            lock.readLock().unlock();
        }
    }

    @Override
    public void close() {
        lock.readLock().lock();
        try{
            channel.close();
        }
        catch (IOException e){}
        finally{
            lock.readLock().unlock();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:58:33+00:00Added an answer on June 17, 2026 at 11:58 am

    Use a ReentrantReadWriteLock. It allows many concurrent readers but only one writer.

    It is not a “pure” NIO solution but it is a primitive which behaves the way you want.

    Beware: use locks like this to avoid deadlocks:

    rwlock.readLock().lock();
    try {
        // do stuff
    } finally {
        rwlock.readLock().unlock();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multithreaded application, where a shared list has write-often, read-occasionally behaviour. Specifically,
I'm seeing what I think is odd behaviour of istream::seekg. Specifically, it appears to
Specifically: Is it assured somehow that all versions of glibc 2.x are binary compatible?
Specifically how can I: Show a button which will let the user browse through
specifically, will working with containers as opposed to the static ObjectFactory let me keep
Specifically I'm looking to insert a mouse out event after a click. So suppose
I was getting some odd behaviour out of a switch block today, specifically I
http://jsfiddle.net/aam7J/ How would I induce div-like behaviour on the input element? Specifically, how can
I'm trying to replicate the behaviour of CUDA's __synchtreads() function in Ruby. Specifically, I
I am looking into WCF specifically in relation to Silverlight. Can someone tell me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.