0

Background

I'm learning Java Socket on my own. I created a Socket Server and Client to run on localhost.

  • Server uses a fixed-size threadpool of size 20 to handle connections.
  • Client creates 50 threads and each sends 1000 connection requests to server one by one.
  • I use a thread-safe object ActiveCount to count the active server threads in each connection.

Behavior

After I start server and client, the ActiveCount object shows the number is stable at 20 for like 5 seconds and the requests are handled at good speed. Then the number starts to drop and reach to 1 active thread only and stuck for 3 seconds. Then due to this low performance, I got java.net.ConnectException: Operation timed out (Connection timed out).

Code

Server

public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(12031);
        ActiveCount threadCount = new ActiveCount(); // for counting current active threads
        Executor pool = Executors.newFixedThreadPool(20);
        while (true) {
            // accept connection and start thread
            Socket clientSocket = serverSocket.accept();
            pool.execute(() -> {
                threadCount.incrementCount();
                try {
                    // Some I/O operations here...
                    BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                    PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

                    String clientID = in.readLine();
                    System.out.println("Client ID is: " + clientID);
                    out.println("Active Server Thread Count = " + threadCount.getCount());
                    out.flush();
                } catch (IOException e) {
                    System.err.println("Server has IO Exception: " + e);
                } finally {
                    threadCount.decrementCount();
                    try { clientSocket.close(); } catch (IOException e) {}
                    System.out.println("Thread exiting");
                }
            });
        }
    }

Client

public class SocketClientMultithreaded {
    static CyclicBarrier barrier; 
    
    public static void main(String[] args){
        String hostName;
        int port;
        int numOfThreads = 50;

        hostName= "localhost";
        port = 12031;  // default port in SocketServer
        barrier = new CyclicBarrier(numOfThreads);

        // start 50 SocketClientThread
        for (int i = 0; i < numOfThreads; i++) {
            Runnable runnable = () -> {
                Socket s;
                for (int j = 0; j < 1000; j++) { // send 1000 requests to the server socket
                    try {
                        s = new Socket(hostName, port);
                        PrintWriter out = new PrintWriter(s.getOutputStream(), true);
                        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
                        out.println("Client ID is " +  Thread.currentThread().getId());
                        System.out.println(in.readLine()); // print the current active thread count sent from server
                    } catch (UnknownHostException e) {
                        System.err.println("Don't know about host " + hostName);
                        break;
                    } catch (IOException e) {
                        System.err.println("I/O connection exception: " + e);
                        break;
                    }
                }
                try {
                    System.out.println("Thread waiting at barrier");
                    barrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    Logger.getLogger(SocketClientThread.class.getName()).log(Level.SEVERE, null, e);
                }
            };
            new Thread(runnable).start(); // start the thread
        }
    }
}

Terminal Terminal output

I suspects there may be some resource problem but the memory usage seems to low. I'm not sure why the thread number would drop and hope to get some help here. Really appreciate it!

2
  • You’re going to get the connection timeouts anyway, trying to open 50,000 connections to a server that only handles 20 at a time and only has so much backlog. Commented Oct 18, 2023 at 22:02
  • hi @user207421, thanks for your reply! Yeah I see why I will have the timeout, but what I don't quite understand is why the number of server threads would drop from 20 to 1. I was expecting that all 20 threads would be busy handling the requests, even though it's not fast enough and I still get timeouts later. Commented Oct 19, 2023 at 17:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.