4,356
社区成员
发帖
与我相关
我的任务
分享
private void run()
{
while (mbIsRunning)
{
try
{
if (mbSelector.select(2000) == 0) continue;
}
catch (Exception e)
{
}
Iterator<SelectionKey> iterator = mbSelector.selectedKeys().iterator();
SelectionKey key;
while (iterator.hasNext())
{
key = iterator.next();
iterator.remove();
if (key.isAcceptable()) handleAccept(key);
else if (key.isReadable()) handleReceive(key);
}
}
depose();
}
private void handleReceive(SelectionKey paKey)
{
if (tpCount % 10000 == 0) tpSTime = System.currentTimeMillis();
SocketChannel channel = (SocketChannel)paKey.channel();
int readBytes;
try
{
if (!channel.isConnected()) return;
mbReadBuffer.clear();
readBytes = channel.read(mbReadBuffer);
if (readBytes > 0)
{
mbReadBuffer.flip();
byte[] bytes = new byte[mbReadBuffer.remaining()];
mbReadBuffer.get(bytes);
String data = new String(bytes, "UTF-8");
++tpCount;
if (tpCount % 10000 == 0)
{
System.out.println(String.format("Received 10000 data : %d",
System.currentTimeMillis() - tpSTime));
}
}
}
catch (Exception e)
{
}
}