English 中文(简体)
SocketChannel.read()区块无限期
原标题:SocketChannel.read() blocks indefinitely

我花了很强的时间,把这.起来。 我有以下法典:

    if (selector.select(1000) <= 0) {
        return;
    }
    Set<SelectionKey> selectionKeys = selector.selectedKeys();
    for (SelectionKey key : selectionKeys) {
        try { 
            SocketEventHandler handler = (SocketEventHandler) key.attachment();
            if (key.isValid() && key.isAcceptable()) {
                handler.becomesAcceptable(key);
            }
            if (key.isValid() && key.isReadable()) {
                handler.becomesReadable(key);
            }

            if (key.isValid() && key.isWritable()) {
                handler.becomesWritable(key);
            }

            if (key.isValid() && key.isConnectable()) {
                handler.becomesConnectable(key);
            }
        } catch (IOException e) {
            key.cancel();
        }
    }
    selector.selectedKeys().clear();

The following Handler Code for案文如下:

synchronized public void becomesReadable(SelectionKey key) throws IOException {
    ByteBuffer temporaryBuffer = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE);
    temporaryBuffer.position(0);
    int read = -1;
    try {
        read = channel.read(temporaryBuffer);
    } catch (IOException e) {
        prefixLogger.debug("Trace:", e);
        close();
    }
    ...
}

And that s the only point where the handler function is ever called. So if I ever get into the becomeReadable() function the channel is in readable state, yet the call to read() blocks without ever returning. Is there something I missed?

问题回答

我不禁要问,为什么这一方法能够同步使用。 使用NIO较好时的同步程度较低。

我也想知道,为什么你不把国际海洋研究组织排除在外,为什么你只是取消钥匙,而不是关闭渠道。 因此,我也想知道,我/奥先生的例外情况是什么,你知道你没有。

分配缓冲费用也非常昂贵:每读一次,你们就应该有一席之地,在分配给该频道的SocketEventHandler案。

您在《刑法》中是否打上了<条码>。

很难告诉你在这里使用哪一种渠道,但我想.SocketChannel是按最初的阻挡模式建立的。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签