日期:2014-05-20 浏览次数:20678 次
public class TerminalServer extends Thread{ private int Port; /** * 接收终端数据缓冲长度 */ public static int TerminalDataSize = 1024; private static DatagramSocket dsocket; private static DatagramPacket dpacket; public TerminalServer(int port) { Port = port; } public void run(){ try{ byte buffer[] = new byte[TerminalDataSize]; TerminalInfo ti = new TerminalInfo(); dsocket = new DatagramSocket(Port); for(;;){ dpacket = new DatagramPacket(buffer,buffer.length); dsocket.receive(dpacket); } }catch(Exception e){ logger.error("终端数据接收线程发生异常",e); } }
TerminalInfo ti = new TerminalInfo(); dsocket = new DatagramSocket(Port); for(;;){ dpacket = new DatagramPacket(new byte[TerminalDataSize],TerminalDataSize); dsocket.receive(dpacket); //... ... }
------解决方案--------------------
public class TerminalServer extends Thread{ private int Port; /** * 接收终端数据缓冲长度 */ public static int TerminalDataSize = 1024; private static DatagramSocket dsocket; private static DatagramPacket dpacket; [color=#FF0000]public static final ConcurrentLinkedQueue<DatagramPacket> udpqueue = new ConcurrentLinkedQueue<DatagramPacket>();[/color] public TerminalServer(int port) { Port = port; } public void run(){ try{ byte buffer[] = new byte[TerminalDataSize]; TerminalInfo ti = new TerminalInfo(); dsocket = new DatagramSocket(Port); for(;;){ dpacket = new DatagramPacket(buffer,buffer.length); dsocket.receive(dpacket); [color=#FF0000]udpqueue.add(packet);[/color] } }catch(Exception e){ logger.error("终端数据接收线程发生异常",e); } }
------解决方案--------------------
那要看你写这个东西是干什么用的,不同需求不同的设计方法
------解决方案--------------------
可以用线程池解决这个问题。