Multithreading 测试多线程UDP服务器(Java)

Multithreading 测试多线程UDP服务器(Java),multithreading,testing,udp,Multithreading,Testing,Udp,我正在实现一个多线程UDP客户机-服务器字典。我想我已经正确地实现了它,但我不知道如何正确地测试它。如果有人有时间,你能看一下我的代码吗 这是我通常运行程序的方式: 如您所见,输出似乎很好。我将线程数保持在最大值(5),因为它是一个“工作池模型”。但是,在UDP中没有“活动连接”,只有发送和接收的数据包。一旦客户机得到它的数据包,线程就关闭了。这种情况发生得非常快,因此我无法同时测试多个客户端的连接。有什么建议吗 我还使用setter来更新线程数。但我使用 “DictServer.decNumT

我正在实现一个多线程UDP客户机-服务器字典。我想我已经正确地实现了它,但我不知道如何正确地测试它。如果有人有时间,你能看一下我的代码吗

这是我通常运行程序的方式:

如您所见,输出似乎很好。我将线程数保持在最大值(5),因为它是一个“工作池模型”。但是,在UDP中没有“活动连接”,只有发送和接收的数据包。一旦客户机得到它的数据包,线程就关闭了。这种情况发生得非常快,因此我无法同时测试多个客户端的连接。有什么建议吗

我还使用setter来更新线程数。但我使用
“DictServer.decNumThreads()”,这是错误的吗

我的代码:

服务器类:

public class DictServer {

private static int threads = 0;

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

    // Connection Parameters
    DatagramSocket socket = null;
    int maxThreads = 5;             // Max threads at any time

    // Retrieve user input
    int serverPort = Integer.parseInt(args[0]);     // Convert string to int
    String dictionaryFile = args[1];

    try {
        // Setup socket
        socket = new DatagramSocket(serverPort);
        System.out.println("Server Started");

        while(true) {
            if(threads < maxThreads) {
                ServerThread server = new ServerThread(socket, dictionaryFile);
                new Thread(server).start();
                threads++;
                System.out.println("Number of threads active: " + threads);
            }               
        }
    }
    catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    finally {
        if(socket != null) 
            socket.close();
    }
}

// Setter for number of active threads
public static void decNumThreads() {
    threads--;
}
}

创建线程的第一个
while(true)
循环是无效的。一旦它达到要启动的最大线程数,它就会坐在那里以100%的使用率烧掉CPU。

您已经编写了一些代码,希望我们测试它吗?自动化您的客户机,从而创建您自己的测试系统。当您遇到问题时,请使用代码、症状、错误消息/异常以及您在调试该问题时所做的一切进行回调。如上所述,我已经运行了客户端并收到了正确的输出。我只是不知道如何测试服务器的线程功能,所以我在这里询问一些细节。我发布了我的代码,因为我认为它会帮助我得到答案。我将尝试使我的客户自动化。
Server Started
Number of threads active: 1
Number of threads active: 2
Number of threads active: 3
Number of threads active: 4
Number of threads active: 5
Thread-0 just run.
Number of threads active: 5
Thread-1 just run.
Number of threads active: 5
Thread-3 just run.
Number of threads active: 5
public class DictServer {

private static int threads = 0;

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

    // Connection Parameters
    DatagramSocket socket = null;
    int maxThreads = 5;             // Max threads at any time

    // Retrieve user input
    int serverPort = Integer.parseInt(args[0]);     // Convert string to int
    String dictionaryFile = args[1];

    try {
        // Setup socket
        socket = new DatagramSocket(serverPort);
        System.out.println("Server Started");

        while(true) {
            if(threads < maxThreads) {
                ServerThread server = new ServerThread(socket, dictionaryFile);
                new Thread(server).start();
                threads++;
                System.out.println("Number of threads active: " + threads);
            }               
        }
    }
    catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }
    finally {
        if(socket != null) 
            socket.close();
    }
}

// Setter for number of active threads
public static void decNumThreads() {
    threads--;
}
}
public class ServerThread implements Runnable {

private DatagramSocket socket = null;
private String dictionaryFile;

// Constructor
public ServerThread(DatagramSocket socket, String dictionaryFile) {
    this.socket = socket;
    this.dictionaryFile = dictionaryFile;
}

@Override
public void run() { 


    byte[] word = new byte[1000];
    byte[] definition = new byte[1000];

    try {
        // Get client request
        DatagramPacket request = new DatagramPacket(word, word.length);
        socket.receive(request);

        // Retrieve definition from dictionary file
        if(word != null) 
            definition = getDefinition(new String(word), dictionaryFile);

        // Put reply into packet, send packet to client
        DatagramPacket reply = new DatagramPacket(definition, definition.length, request.getAddress(), request.getPort());
        socket.send(reply);

    }
    catch (Exception e) {
        System.out.println("Error: " + e.getMessage());
    }

    System.out.println(Thread.currentThread().getName() + " just run.");
    DictServer.decNumThreads();
}