我有一個插座占用了計算機(jī) 100% 的 CPU。每 30 秒有 150 個客戶端非同步地向服務(wù)器發(fā)送消息。有誰知道如何解決這個問題?下面是我的 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 類(實(shí)用程序類)獲取客戶端發(fā)送的字符串并將其保存在數(shù)據(jù)庫中。在我的 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貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個贊
我通過刪除部分 "while (!in.ready()) {/ System.out.println("!in.ready()"); /}" 并在末尾創(chuàng)建一個 "Thread.sleep"解決了這個問題嘗試塊
添加回答
舉報
0/150
提交
取消