我有一个插座占用了计算机 100% 的 CPU。每 30 秒有 150 个客户端非同步地向服务器发送消息。有谁知道如何解决这个问题?下面是我的 ServerSocket 类public class Servidor { static ExecutorService es; public static void main(String[] args) throws Exception { es = Executors.newFixedThreadPool(150); ServerSocket servidor = new ServerSocket(2010); while (true) { Socket soquete = null; try { System.out.println("Aguardando cliente: "); soquete = servidor.accept(); System.out.println("Cliente Conectado: "); es.execute(new Conexao(soquete)); } catch (Exception e) { e.printStackTrace(); } } }}Conexao 类(实用程序类)获取客户端发送的字符串并将其保存在数据库中。在我的 Conexao 课程下方public class Conexao implements Runnable{ Socket soquete; int contador = 0; public Conexao(Socket soquete) { super(); this.soquete = soquete; } @Override public void run(){ BufferedReader in = null; try{ in = new BufferedReader(new InputStreamReader(soquete.getInputStream())); while (!in.ready()) {/*System.out.println("!in.ready()");*/} String str =in.readLine(); System.out.println("Rodando Thread"+Thread.currentThread().getName() + " : texto: " + str); }finally{ ... if(soquete != null){ try { soquete.close(); } catch (IOException e) { // TODO Auto-generated catch block } } } }}
1 回答
慕虎7371278
TA贡献1802条经验 获得超4个赞
我通过删除部分 "while (!in.ready()) {/ System.out.println("!in.ready()"); /}" 并在末尾创建一个 "Thread.sleep"解决了这个问题尝试块
添加回答
举报
0/150
提交
取消