NutzCN Logo
问答 socket服务占用cpu过高 怎么解决啊 求大神支招
发布于 2520天前 作者 qq_d553d34d 7223 次浏览 复制 上一个帖子 下一个帖子
标签: socket

开启socket服务 while true循环监听连接 然后 开启子线程去处理 ;不到一天的时间 cpu使用率就达到了 100%;
跪求大神指点迷津
服务端:
public void init() {
try {
ServerSocket serverSocket = new ServerSocket(port);
while (true) {
// 从请求队列中取出一个连接
Socket client = serverSocket.accept();
// 处理这次连接
new HandlerThread(client);
}
} catch (Exception e) {
System.out.println("服务器异常: " + e.getMessage());
}
}

```
线程
public class HandlerThread implements Runnable {

    private Socket socket;

    public HandlerThread(Socket client) {
       socket = client;
       new Thread(this).start();
    }

    public void run() {

       while (true) {
         try {
          InputStream inputStream = socket.getInputStream();
          byte[] buf = new byte[1100];
          inputStream.read(buf);
          String msg = new String(buf, "utf8");
8 回复

HandlerThread 完整代码

好的 就是接收 锁的信息 判断什么指令 然后执行相应操作

	public class HandlerThread implements Runnable {

		private Socket socket;

		public HandlerThread(Socket client) {
			socket = client;
			new Thread(this).start();
		}

		public void run() {
			
			while (true) {
				try {
					InputStream inputStream = socket.getInputStream();
					byte[] buf = new byte[1100];
					inputStream.read(buf);
					String msg = new String(buf, "utf8");
//					Logs.get().debug("msg==="+msg);
					if (msg.contains("HTBT")) {
						String imei = msg.split(",")[1];
						addActionMap(imei, this);
						feedBack(imei); // 心跳反馈
					}

					if (msg.contains("WARNING")) {
						System.out.println("========震动报警===");
						String imei = msg.split(",")[1];
						shakeAlert(imei);
						Logs.get().debug("shake");
					}
					
					if (msg.contains("125") && !msg.contains("N") && !msg.contains("E") ) {
						String[] location = msg.split(",");
						if (msg.contains("fbffffff") || msg.contains("ffffffff")) {
							String imei = location[1];
							String baterry = location[13];
							lowbattery(imei, baterry);
							
						}
					}

					if (msg.contains("LOCK")) {
						String imei = msg.split(",")[1];
						if (msg.contains("ffffffff")) { // off
						//	org.nutz.http.Response response = Http.get(LOCK_STATUS + "imei=" + imei + "&onoroff=off");
							changeLockStatus(imei, "off");
							
				//			Logs.get().debug(Json.toJson(response.getContent()));
						}
						if (msg.contains("fbffffff")) { // on
							changeLockStatus(imei, "on");

						}
					}
					// 接收地理信息
					if (msg.contains("A") && msg.contains("N") && msg.contains("E")) {
					
						String[] location = msg.split(",");
						String imei = location[1];
						String lat = location[5];
						String log = location[7];
						String baterry = location[13];
						
						
						changeBikePostion(imei, lat, log, baterry);

					}
				} catch (Exception e) {
					System.out.println("errrr");
					System.out.println("服务器 run 异常: " + e.getMessage());
				}
			}
		}

		public void sendLock(String imei) {
			try {
				OutputStream outputStream = socket.getOutputStream();
				String lockCommand = "*HQ," + imei + ",LOCK,1,1,"+BurroKit.getTotalSecondsOfDay()+",1"+"#";
				Logs.get().debug("unlock===="+lockCommand);
				outputStream.write(lockCommand.getBytes());
				outputStream.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		public void sendLock2(String imei) {
			try {
				OutputStream outputStream = socket.getOutputStream();
				OutputStreamWriter osw = new OutputStreamWriter(outputStream, "UTF-8");
				BufferedWriter bw = new BufferedWriter(osw);
				PrintWriter pw = new PrintWriter(bw);
				String lockCommand = "*HQ," + imei + ",LOCK,1,1,"+BurroKit.getTotalSecondsOfDay()+"#";
				pw.write(lockCommand);
				pw.close();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		/**
		 * 心跳反馈
		 * 
		 * @param imei
		 */
		public void feedBack(String imei) {
			// *HQ,imei,HTBT,[时间]#
			try {
				OutputStream outputStream = socket.getOutputStream();
				String lockCommand = "*HQ," + imei + ",HTBT," + BurroKit.getCurrTimeStr() + "#";
				outputStream.write(lockCommand.getBytes());
				outputStream.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		/**
		 * 重启
		 * @param imei
		 */
		public void reboot(String imei) {
			try {
				OutputStream outputStream = socket.getOutputStream();
				String lockCommand = "*HQ," + imei + ",REBOOT," + BurroKit.getCurrTimeStr() + "#";
				outputStream.write(lockCommand.getBytes());
				outputStream.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		

		
	}
				} catch (Throwable e) {
					System.out.println("errrr");
					System.out.println("服务器 run 异常: " + e.getMessage());
                                        break; // 跳出循环
				}

好的 我试试 跪谢啦

多问一下 这是什么原理啊

里面线程, 没有任何退出机制? 没看看任何break/return语句. 起码抛异常就应该退出了吧.

哦 好的 我试验一下

楼主问题解决了吗,怎么解决的,同样困境,求助

添加回复
请先登陆
回到顶部