亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java Socket 占用 100% CPU

Java Socket 占用 100% CPU

Cats萌萌 2023-05-10 15:19:09
我有一個插座占用了計算機 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"解決了這個問題嘗試塊



查看完整回答
反對 回復 2023-05-10
  • 1 回答
  • 0 關注
  • 270 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號