简体   繁体   English

使用TCP / IP套接字进行多线程处理

[英]Multithreading with TCP / IP socket

I am working on sockets in Java. 我正在使用Java中的套接字。 The thing is we are acquiring signal from hardware device and if the device is not scanned after certain interval (200ms) is hangs. 问题是我们正在从硬件设备获取信号,并且如果在某个间隔(200ms)之后没有扫描设备。 So I need to speed up this operation. 所以我需要加快这个操作。 How can I use multitasking to speed up the following code. 如何使用多任务处理来加速以下代码。

The no of rows here is usually in the range of 250. 这里的行数通常在250的范围内。

    sock = new Socket("localhost", def.PORT);

    OutputStream outStream = sock.getOutputStream();
    DataOutputStream outDataStream = new DataOutputStream(outStream);
    /**************** Start Command for CystometryEMG *****************/
    outDataStream.writeInt(size);
    outDataStream.writeInt(command);
    outDataStream.writeBytes(msg);

    InputStream input = new BufferedInputStream(sock.getInputStream());
    reader = new DataInputStream(input);

    sock.setSoTimeout(def.SOCKET_READ_TIME_OUT);
    if (sock.isClosed())
        return;
    size = reader.readInt();
    command = reader.readInt();
    errorCode = reader.readInt();
    NoofCols = reader.readInt();
    NoOfRows = reader.readInt();

    /************** Now get the input from the device *************/

    for (int i = 0; i < NoOfRows; i++)
        arrPb[i] = reader.readDouble();

    for (int i = 0; i < NoOfRows; i++)
        arrPv[i] = reader.readDouble();

    for (int i = 0; i < NoOfRows; i++)
        arrV[i] = reader.readDouble();

    for (int i = 0; i < NoOfRows; i++)
        arrVn[i] = reader.readDouble();

    sock.close();

Decouple scanning the device from all other operations. 将设备与所有其他操作分离扫描。 Split this into two threads. 将其拆分为两个线程。 One thread (frontend) handles nothing but scanning the device and saving the data received into a queue. 一个线程(前端)只处理扫描设备并将接收到的数据保存到队列中。 This thread should run with maximum priority. 该线程应以最高优先级运行。 The second thread (backend) takes data from the queue and does whatever you want with it, at leisure. 第二个线程(后端)从队列中获取数据,并随意做任何你想做的事情。 You will probably need multiple backend threads, and after you get this working you'll need to tune the number of backend threads to ensure that you can process requests faster than they're received from the device, otherwise your queue will grow too large. 您可能需要多个后端线程,并且在您开始工作之后,您需要调整后端线程的数量,以确保您可以比从设备接收请求更快地处理请求,否则您的队列将变得太大。

Note however that Java is generally not a realtime language and even with a dedicated thread it may be possible for the frontend thread to take more time to get scheduled that you might like. 但请注意,Java通常不是实时语言,即使使用专用线程,前端线程也可能需要更多时间来安排您可能喜欢的语言。 You might do a web search for "realtime java" to see if any of the existing realtime JVMs out there can help you. 您可以对“realtime java”进行Web搜索,以查看是否有任何现有的实时JVM可以帮助您。

Regardless, don't even think about running this on anything less than a 4-core CPU 无论如何,甚至不要考虑在低于4核的CPU上运行它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM