我有一个用于 UDP 数据包的 Java 数据包侦听器线程以及 2-3 个其他线程。
直到今天它运行良好,但现在进程 javaw.exe 已开始使用 CONSTANT 50% CPU。
这是我的代码。
public class PacketListenerThread implements Runnable {
private SocketAddress receivedSocketAddress;
private DatagramChannel channel;
private ExecutorService pool;
public PacketListenerThread(DatagramChannel channel, ExecutorService pool) {
this.channel = channel;
this.pool = pool;
}
@Override
public void run() {
while (true) {
receivedSocketAddress = null;
ByteBuffer recvbuf = ByteBuffer.allocate(1400);
recvbuf.clear();
try {
receivedSocketAddress = channel.receive(recvbuf);
} catch (IOException e) {
e.printStackTrace();
}
if (receivedSocketAddress != null) {
pool.submit(new PacketHandlerRunnable(new TaskObject(receivedSocketAddress, recvbuf)));
}
}
}
}
我已经停止了所有其他线程,但这个线程仍然使用 "CONSTANT" 50% CPU 。