Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java thread blocks while registering channel with selector while select() is called. What to do?

I have a basic question. Why and how SelectableChannel's register method can be in blocking call. Let me provide a scenario.

I have created a Selector object in class Register as follows.

private static Selector selector = Selector.open();

I also have a method in same class(Register) to register the channel with the selector.

public static SelectionKey registerChannel(SelectableChannel channel, int ops)
                             throws IOException {
   channel.configureBlocking(false);
   return channel.register(selector, ops);
}

And there is another class named Request, which has method which reads the data from channels, processes and calls following method to register the channel.

selectonKey = Register.register(socketChannel, SelectionKey.OP_READ);

Here at this point the thread is blocked, not giving clue of what it is waiting for. I have verified that the selector is open. Please provide me some help to understand how can I resolve this. Is there any lock that I can release.

Any input would be appreciated.

Adding to what I described. Further tests revealed that if the Register.register method is called from the same thread, it is able to register but after that if some other thread tries to invoke the method, thread doesn,t move ahead.

like image 865
Nilesh Avatar asked Jun 29 '09 08:06

Nilesh


People also ask

What is blocking of thread in Java?

Blocking methods in java are the particular set of methods that block the thread until its operation is complete. So, they will have to block the current thread until the condition that fulfills their task is satisfied. Since, in nature, these methods are blocking so-called blocking methods.

What is selector method in Java?

Last update: 2021-03-10. The Java NIO Selector is a component which can examine one or more Java NIO Channel instances, and determine which channels are ready for e.g. reading or writing. This way a single thread can manage multiple channels, and thus multiple network connections.

What is a selector thread?

A selector provides a mechanism for monitoring one or more NIO channels and recognizing when one or more become available for data transfer. This way, a single thread can be used for managing multiple channels, and thus multiple network connections.

How to use selector in Java?

A selector may be created by invoking the open method of this class, which will use the system's default selector provider to create a new selector. A selector may also be created by invoking the openSelector method of a custom selector provider. A selector remains open until it is closed via its close method.


2 Answers

This is a basic feature of most NIO implementations that isn't obvious from the documentation.

You need to make all register calls from the same thread that is doing your selecting or deadlocks will occur. Usually this is done by providing a queue of registrations/deregistrations/interest-changes that is written to and then selector.wakeup() is called. When the selecting thread wakes up it checks the queue and performs any requested operations.

like image 106
Darron Avatar answered Nov 14 '22 05:11

Darron


You need to use a lock and manually synchronize.

In the same thread you are running the selector loop have a ReentrantLock:

final ReentrantLock selectorLock = new ReentrantLock();

Then when you need to register with the selector do something like this:

selectorLock.lock();
try {
    selector.wakeup();
    socketChannel.register(selector, ops);
} finally {
    selectorLock.unlock();
}

Finally, during your loop that you are calling accept(), something like this:

selectorLock.lock();
selectorLock.unlock();

selector.select(500);

And then continue on with the rest of your logic.

This construct guarantees that the register() call will not block by ensuring that there is never another select() between corresponding wakeup() and register() calls.

like image 21
Kevin Herron Avatar answered Nov 14 '22 07:11

Kevin Herron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!