以下是我的代码,问题出在多线程执行时总花费很长时间,大概要6秒左右,很郁闷,不知道怎么回事,请各位大侠赐教!
class WorkThread1 implements Runnable{
private DatagramPacket packetIn;
private DatagramSocket s;
private InetSocketAddress remoteAddress;
public WorkThread1(DatagramPacket packetIn,DatagramSocket s,InetSocketAddress remoteAddress){
this.packetIn=packetIn;
this.s=s;
this.remoteAddress=remoteAddress;
}
public void run(){
 
         //校验客户端
        
InetSocketAddress localAddress = (InetSocketAddress)s.getLocalSocketAddress();//IP 地址 + 端口号

String secret = getSharedSecret(remoteAddress);

try{
if (secret == null) {
if (logger.isInfoEnabled())
logger.info("ignoring packet from unknown client " + remoteAddress + " received on local address " + localAddress);
//continue;

}//解析包

RadiusPacket request = makeRadiusPacket(packetIn, secret);
System.out.println("111111111111111111111111111111");
if (logger.isInfoEnabled())
logger.info("received packet from " + remoteAddress + " on local address " + localAddress + ": " + request); //处理包
logger.trace("about to call RadiusServer.handlePacket()");
RadiusPacket response = handlePacket(localAddress, remoteAddress, request, secret);

//发送回应
if (response != null) {
if (logger.isInfoEnabled())
logger.info("send response: " + response);
DatagramPacket packetOut = makeDatagramPacket(response, secret, remoteAddress.getAddress(), packetIn.getPort(), request);
s.send(packetOut);
} else
logger.info("no response sent");
} catch (SocketTimeoutException ste) {
// this is expected behaviour
logger.trace("normal socket timeout");
} catch (IOException ioe) {
// error while reading/writing socket
logger.error("communication error", ioe);
} catch (RadiusException re) {
// malformed packet
logger.error("malformed Radius packet", re);
}
        

}
}
public void initThreadPool(int minPoolSize, int maxPoolSize) {
CORE_POOL_SIZE = minPoolSize;
MAX_POOL_SIZE = maxPoolSize;
serverThreadPool = new ThreadPoolExecutor(CORE_POOL_SIZE,
MAX_POOL_SIZE, KEEPALIVE_TIME, TIME_UNIT, workQueue,
rejectedExecutionHandler);
// pool = Executors.newFixedThreadPool(1000);
}protected void listen(DatagramSocket s) {
initThreadPool(30,50);
DatagramPacket packetIn = new DatagramPacket(new byte[RadiusPacket.MAX_PACKET_LENGTH], RadiusPacket.MAX_PACKET_LENGTH);
//构造 DatagramPacket,用来接收长度为RadiusPacket.MAX_PACKET_LENGTH 的数据包
while (true) {
try {
//接收包 
try {
logger.trace("about to call socket.receive()");    
s.receive(packetIn);
// System.out.println("*******"+packetIn);
if (logger.isDebugEnabled())
logger.debug("receive buffer size = " + s.getReceiveBufferSize());
} catch (SocketException se) {
if (closing) {
//结束线程
logger.info("got closing signal - end listen thread");
return;
} else {
//重新尝试 s.receive()
logger.error("SocketException during s.receive() -> retry", se);
continue;
}
}
InetSocketAddress remoteAddress = new InetSocketAddress(packetIn.getAddress(), packetIn.getPort());
serverThreadPool.execute(new WorkThread1(packetIn,s,remoteAddress));
} catch (SocketTimeoutException ste) {
// this is expected behaviour
logger.trace("normal socket timeout");
} catch (IOException ioe) {
// error while reading/writing socket
logger.error("communication error", ioe);

}
}

解决方案 »

  1.   

    给几个建议:
    1、下次发代码用论坛的格式器排版一下,不然很难给你分析
    2、你贴出来的这个文件似乎跑不起来啊,如果你希望解决某些问题,可以把没用的代码去掉,简单的给个说明代码就好,但整个程序框架最好完整
    3、至于性能问题,因为看不到完整代码,我只能猜测有几种可能啦:比如线程池的配置、网络状态、数据处理等都有可能,其实你只要断点跟一下这几个地方一般都能发现问题代码的小问题就不多说了,try中套try不是个好习惯,另外那个closing变量也不知道哪里声明的,listen方法是主动调用的么?
      

  2.   


    为什么try中套try不是个好习惯?这样能有效的防止未关闭的对象发生.try {
      Connection conn = DriverManager.getConnection(url,user,pass);
      try {  
        PrepareStatement stmt = conn.prepareStatement(sql);
      } finnaly {
        conn.close();
      }
      

  3.   

    呵呵,楼上的朋友也有道理,只是我建议不要try中套try,因为try和catch在JVM中要维护这个结构所花费的开销是非常大的!
    我们尽量把流程能购精简,你可以做很多catch,但是最好不要开很多个try