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

时间:2012-08-10 04:03:27

标签: multithreading testing udp

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

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

java DictServer <port> <dictionary file name>
java DictClient localhost <port> <word to search>

这是服务器的输出(客户端已经在这里运行了3次):

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

如您所见,输出似乎很好。我将线程数保持在最大值(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--;
}
}

线程类:

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();
}

1 个答案:

答案 0 :(得分:0)

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