gpt4 book ai didi

java - 为什么Java nio消耗CPU过多?

转载 作者:行者123 更新时间:2023-12-01 18:40:07 26 4
gpt4 key购买 nike

我使用Java nio开发了一个服务器。并且建立的连接很少(60-70)。虽然只建立了连接,没有数据流动,但它消耗了我30%的CPU(i7第8代12核)。如果数据流动,则为80-90%。当我使用jvisualvm分析程序时,似乎只使用了selector.select()方法。我想知道如何才能减少 CPU 使用率。

  private void startServer() throws IOException {
this.selector = Selector.open();
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.configureBlocking(false);

// retrieve server socket and bind to port
listenAddress = new InetSocketAddress("localhost", 8090);
serverChannel.socket().bind(listenAddress);
serverChannel.register(this.selector, SelectionKey.OP_ACCEPT);

System.out.println("Server started...");

while (true) {
// wait for events
this.selector.select();

//work on selected keys
Iterator keys = this.selector.selectedKeys().iterator();
while (keys.hasNext()) {
SelectionKey key = (SelectionKey) keys.next();

// this is necessary to prevent the same key from coming up
// again the next time around.
keys.remove();

if (!key.isValid()) {
continue;
}

if (key.isAcceptable()) {
this.accept(key);
}
else if (key.isReadable()) {
this.read(key);
}
else if (key.isWritable()) {
this.write(key);
}
else if (key.isConnectable()) {
this.connect(key);
}
}
}
}


private void accept(SelectionKey key) throws IOException {
ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();
SocketChannel channel = serverChannel.accept();
channel.configureBlocking(false);

// register channel with selector for further IO
dataMapper.put(channel, new ArrayList());
channel.register(this.selector, SelectionKey.OP_READ);
}

//read from the socket channel
private void read(SelectionKey key) throws IOException {
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
int numRead = -1;
numRead = channel.read(buffer);

if (numRead == -1) {
this.dataMapper.remove(channel);
channel.close();
key.cancel();
return;
}

processBuffer(buffer);
}

public void startClient() throws IOException, InterruptedException {
String remoteHost = "localhost";
int remotePort = 8090
SocketChannel client = SocketChannel.open();
client.configureBlocking(false);

client.bind(new InitSocketAddress(0);
client.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
client.connect(new InetSocketAddress(remoteHost, remotePort));

client.register(this.selector, SelectionKey.OP_CONNECT);
}

public void connect(SelectionKey key){
SelectableChannel channel = key.channel();
SocketChannel socketChannel = (SocketChannel) channel;

boolean isConnected = socketChannel.finishConnect();
if(!isConnected)
System.out.println("Not Connectted!");

}

public void write(SelectionKey key){
SocketChannel socketChannel = (SocketChannel) channel;

ByteByffer buffer = getBuffer();

while(buf.remaining()>0){
socketChannel.write(buf)
}

//we wrote the data, now we interested in waiting for data.
key.interestOps(SelectionKey.OP_READ);

}

最佳答案

如果您的 select() 返回 0,就会发生这种情况。您可以打印一些日志来检查这一点。

请注意,连接后应取消注册 OP_CONNECT。

关于java - 为什么Java nio消耗CPU过多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59950368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com