on freebsd, java's nio has muti-thread bug, work fine at linux version openjdk. How-To-Repeat: 1, create and run the code(see below) 2, run command: nc localhost 9999 3, send anything via nc (input a char and enter key) 4, you can see the connect its close 5, run command again: nc localhost 9999 6, send anything via nc (input a char and enter key) 7, dead here... but openjdk on linux its work fine (disconnect again) the java source code: import java.net.InetSocketAddress; import java.net.ServerSocket; import java.nio.ByteBuffer; import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Iterator; import java.util.Set; public class Test2 { public static void main(final String[] args) throws Exception { final ServerSocketChannel sChannel = ServerSocketChannel.open(); sChannel.configureBlocking(false); final ServerSocket sSock = sChannel.socket(); sSock.bind(new InetSocketAddress(9999), 10); final SelectionKey acpKey = sChannel.register(Selector.open(), SelectionKey.OP_ACCEPT); final ByteBuffer buff = ByteBuffer.allocate(8192); for (;;) { if (acpKey.selector().select() == 0) continue; final Set<SelectionKey> rdyKey = acpKey.selector().selectedKeys(); for (final Iterator<SelectionKey> iter = rdyKey.iterator(); iter .hasNext();) { final SelectionKey key = iter.next(); iter.remove(); ; if (key.isValid()) { if (key.isAcceptable()) { final ServerSocketChannel ssc = (ServerSocketChannel) key .channel(); final SocketChannel sc = ssc.accept(); sc.configureBlocking(false); sc.register(key.selector(), SelectionKey.OP_READ); } else if (key.isReadable()) { final SocketChannel sc = (SocketChannel) key.channel(); final int ir = sc.read(buff); System.out.println(new String(buff.array(), 0, ir)); buff.position(0); new Thread() { @Override public void run() { try { Thread.sleep(1000); key.cancel(); key.attach(null); sc.socket().close(); sc.close(); } catch (final Exception e) { e.printStackTrace(); } } }.start(); // bug here, but work fine if .run() } else if (key.isWritable()) { } } } } } }
Responsible Changed From-To: freebsd-i386->freebsd-java Fix synopsis and assign.
back to pool