java udp 服务器 100% cpu
java udp server 100% cpu
我有一个 UDP 服务器,它以每秒 40 pkts 的速度接收数据包。
主循环如下:
public void serve() {
while(true) {
ByteBuffer bytes = ByteBuffer.allocate(1024);
bytes.clear();
channel.receive(bytes);
THandler th = new THandler(bytes);
th.start();
}
}
频道初始化:
private final DatagramChannel channel ;
channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(port));
classTHandler 扩展了一个线程class,它根据正则表达式过滤消息,然后从匹配的消息中找到一个 id。
然后将此 ID 与订阅者列表(大约 300k)进行比较。接着是数据库 select 然后是更新。
public void run() {
if (!this.isValidLog()){ //does a regular expression match
return;
}
String subsId = this.getSubscriberId(); // extracts the id from message
if (this.isServiceSubscribed(subsId)) { // compares in a list of subscribers
usageDetails = this.getServiceUsage(subsId); // db Query
if(usageDetails.isFeatureAvailable()){
usageDetails.updateUsageCounter(); // db Update
}
}
}
我也尝试过使用 ExecutorService。但问题是我 运行 在 CPU 时间里有 99.6% 或更多的用户。
欢迎就如何改进服务器和代码的性能提出任何意见。
我有一个 UDP 服务器,它以每秒 40 pkts 的速度接收数据包。 主循环如下:
public void serve() {
while(true) {
ByteBuffer bytes = ByteBuffer.allocate(1024);
bytes.clear();
channel.receive(bytes);
THandler th = new THandler(bytes);
th.start();
}
}
频道初始化:
private final DatagramChannel channel ;
channel = DatagramChannel.open();
channel.socket().bind(new InetSocketAddress(port));
classTHandler 扩展了一个线程class,它根据正则表达式过滤消息,然后从匹配的消息中找到一个 id。 然后将此 ID 与订阅者列表(大约 300k)进行比较。接着是数据库 select 然后是更新。
public void run() {
if (!this.isValidLog()){ //does a regular expression match
return;
}
String subsId = this.getSubscriberId(); // extracts the id from message
if (this.isServiceSubscribed(subsId)) { // compares in a list of subscribers
usageDetails = this.getServiceUsage(subsId); // db Query
if(usageDetails.isFeatureAvailable()){
usageDetails.updateUsageCounter(); // db Update
}
}
}
我也尝试过使用 ExecutorService。但问题是我 运行 在 CPU 时间里有 99.6% 或更多的用户。
欢迎就如何改进服务器和代码的性能提出任何意见。