Note that there are some explanatory texts on larger screens.

plurals
  1. POjava nio Selector wakeup
    primarykey
    data
    text
    <p>Please point/provide me an working example of <code>selector.wakeup();</code> method between two threads.</p> <p>I tried to create a simple program where a thread is waiting on selector.select() method. Second thread creates some sockets and tries to register with the selector; on which the first thread is blocked. </p> <p>Hence I need to use selector's <code>wakeup</code> method, but somehow the first thread isnt doesn't come out of blocking mode.</p> <p>The javadoc of wakeup method states:</p> <blockquote> <p>If another thread is currently blocked in an invocation of the Selector.select() or Selector.select(long) methods then that invocation will return immediately.</p> </blockquote> <p>P.S There are few other work-around; one of them is select(timeout) but I am trying to figure out where the mistake is.</p> <p>The psuedo-code:</p> <p><em>FIRST THREAD:</em></p> <pre><code>static Selector selector = Selector.open(); while(true) { int n = selectorGlobal.select(); selectorKeySet = selectorGlobal.selectedKeys().iterator(); while (selectorKeySet.hasNext()) { selectionKey = selectorKeySet.next(); if (selectionKey.isReadable()) { //do something } if(selectionKey.isAcceptable()) { //accept } } } </code></pre> <p><em>SECOND THREAD:</em></p> <pre><code>while (itr.hasNext()) { data = (String) itr.next(); String IP = data.get(0); String Port = data.get(1); SocketChannel socketChannel = SocketChannel.open(); socketChannel.configureBlocking(true); boolean isConnected = socketChannel.connect(new InetSocketAddress(IP, Port)); ClassName.selector.wakeup(); SelectionKey selectionKey = SelectSockets.registerChannel(ClassName.selector, socketChannel, SelectionKey.OP_READ); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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