多线程客户端套接字在JAVA中占用100%的CPU。

huangapple 未分类评论51阅读模式
标题翻译

Multithread Client Socket has CPU 100% in JAVA

问题

我有一个严重的问题。实际上我有一个客户端程序,每秒从服务器接收大约 2000 个 JSON 字符串(接收数据速率非常高)。我必须用多个端口连接到服务器,每个端口都必须使用一个线程进行处理。另一方面,我必须使用 Read() 函数逐个字符地获取数据,因为从服务器发送的数据如何发送并不清楚(ReadLine 和 ReadUTF 有时不起作用,因为服务器从一些真实传感器获取数据,这些数据是不可预测的)。

主要问题是,当我运行程序时,程序开始以高速接收数据。但 Java 运行时的 CPU 利用率达到 100%,在运行后 30 秒后获取数据的速度变慢。
以下是源代码:

import java.io.*;
import java.net.*;

public class Client implements Runnable{

    private Socket clientSocket = null;
    private BufferedReader is = null;
    private int port;

    public Client(int port){
        this.port = port;
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        Client a = new Client(7002);
        Thread one = new Thread(a);

        Client b = new Client(7003);
        Thread two = new Thread(b);

        Client c = new Client(7004);
        Thread three = new Thread(c);

        one.start();
        two.start();  
        three.start();
    }

    public void run() {
        System.out.println("hi from thread " + port);
        try {
            clientSocket = new Socket("192.168.2.156", port);
            is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        } 
        catch(IOException e){
            System.out.println(e);
        }
     
        try{ 
            //***************************************Port 7002*****************************************            
            while(port == 7002){
                String str = "";
                StringBuffer sbuffer = new StringBuffer("");
                while (is.ready()) {                    
                    char[] c = new char[]{1};
                    is.read(c);
                    sbuffer.append(c);
                    if (c[0]=='}') 
                    {
                        str = sbuffer.toString();
                        if ((str.charAt(0)=='{') && (str.charAt(str.length()-1)=='}')) 
                        {
                            System.out.println("Received data from port 7002: "+str);
                            break;
                        } else {
                            System.out.println("Received Data Lost!!!!");
                        }  
                    }
                }                      
            }
            //***************************************Port 7003*****************************************            
            while(port == 7003){
                String str = "";
                StringBuffer sbuffer = new StringBuffer("");
                while (is.ready()) {                    
                    char[] c = new char[]{1};
                    is.read(c);
                    sbuffer.append(c);
                    if (c[0]=='}') 
                    {
                        str = sbuffer.toString();
                        if ((str.charAt(0)=='{') && (str.charAt(str.length()-1)=='}')) 
                        {
                            System.out.println("Received data from port 7003: "+str);
                            break;
                        } else {
                            System.out.println("Received Data Lost!!!!");
                        }  
                    }
                }                      
            }
            //***************************************Port 7004*****************************************            
            while(port == 7004){
                String str = "";
                StringBuffer sbuffer = new StringBuffer("");
                while (is.ready()) {                    
                    char[] c = new char[]{1};
                    is.read(c);
                    sbuffer.append(c);
                    if (c[0]=='}') 
                    {
                        str = sbuffer.toString();
                        if ((str.charAt(0)=='{') && (str.charAt(str.length()-1)=='}')) 
                        {
                            System.out.println("Received data from port 7004: "+str);
                            break;
                        } else {
                            System.out.println("Received Data Lost!!!!");
                        }  
                    }
                }                      
            }   
        }
        catch (IOException e) {
            System.out.println(e);
        }
    }
}

请帮助我解决这个问题。

英文翻译

I have a serious problem. Actually I have a Client program which receive approximately 2000 JSON strings per second from a Server (Data rate of receive data is very high). I have to connect to server with several Ports which every port must handles with a Thread. On the other side, I have to get the data with Read() function char by char, because it is not clear at all how it is sent from server (ReadLine and ReadUTF sometimes don't work because server get data from some Real Sensors and they are unpredictable).

The main problem is When I run the program, the program begins to receive data at high speeds. But CPU goes to 100% in Java Run time and the speed of get data goes to slow after 30 second.
the source code is here:

import java.io.*;
import java.net.*;

public class Client implements Runnable{

private Socket clientSocket = null;
private BufferedReader is = null;
private int port;

public Client(int port){
    this.port = port;
}

public static void main(String[] args) throws IOException, InterruptedException {

    

    Client a = new Client(7002);
    Thread one = new Thread(a);

    Client b = new Client(7003);
    Thread two = new Thread(b);
    Client c = new Client(7004);
    Thread three = new Thread(c);

    
    
    one.start();
    two.start();  
    three.start();

}

public void run() {
    System.out.println("hi from thread" + port);
    try {
        clientSocket = new Socket("192.168.2.156", port);
        is = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        
    } 
    catch( IOException e){
        System.out.println(e);
    }
 
    try{ 
     //***************************************Port 7002*****************************************            
        while(port == 7002){
            String str = "";
            StringBuffer sbuffer = new StringBuffer("");
            while (is.ready()) {                    
                char[] c = new char[]{1};
                is.read(c);
                sbuffer.append(c);
                if (c[0]=='}') 
                {
                    str = sbuffer.toString();
                    if ((str.charAt(0)=='{')&(str.charAt(str.length()-1)=='}')) 
                    {
                        System.out.println("Received data from port 7002: "+str);
                        break;
                    }else{
                        System.out.println("Received Data Lost!!!!");
                    }
                   
                }
            }
                    
        }
       //***************************************Port 7003*****************************************            
        while(port == 7003){
            String str = "";
            StringBuffer sbuffer = new StringBuffer("");
            while (is.ready()) {                    
                char[] c = new char[]{1};
                is.read(c);
                sbuffer.append(c);
                if (c[0]=='}') 
                {
                    str = sbuffer.toString();
                    if ((str.charAt(0)=='{')&(str.charAt(str.length()-1)=='}')) 
                    {
                        System.out.println("Received data from port 7003: "+str);
                        break;
                    }else{
                        System.out.println("Received Data Lost!!!!");
                    }
                   
                }
            }
                    
        }
       //***************************************Port 7004*****************************************            
        while(port == 7004){
            String str = "";
            StringBuffer sbuffer = new StringBuffer("");
            while (is.ready()) {                    
                char[] c = new char[]{1};
                is.read(c);
                sbuffer.append(c);
                if (c[0]=='}') 
                {
                    str = sbuffer.toString();
                    if ((str.charAt(0)=='{')&(str.charAt(str.length()-1)=='}')) 
                    {
                        System.out.println("Received data from port 7004: "+str);
                        break;
                    }else{
                        System.out.println("Received Data Lost!!!!");
                    }
                   
                }
            }
                    
        }
        
    }
    catch (IOException e) {
        System.out.println(e);
    }
}

}

Please Help me to solve the problem.

huangapple
  • 本文由 发表于 2020年5月30日 23:31:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/62104697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定