简体   繁体   中英

Problem with cleaning up a thread pool

I have a server in Java which is multithreading, and I've created a thread pool for it. Now everything goes well and my server accepts and reads data from the clients that connect to it, but I don't know really how to clean up the sockets after the connections are closed.

So here is my code:

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub

    ThreadPooledServer server = new ThreadPooledServer(queue,7001);
    new Thread(server).start();
}

ThreadPooledServer class:

public class ThreadPooledServer implements Runnable {
    protected ExecutorService threadPool = Executors.newFixedThreadPool(5);

public ThreadPooledServer(BlockingQueue queue,int port) {
    this.serverPort = port;
    this.queue=queue;
}

    public void run() {
        openServerSocket();

        while (!isStopped()) {
            Socket clientSocket = null;
            try {
                clientSocket = serverSocket.accept();
                clientconnection++;
            } catch (IOException e) {
                if (isStopped()) {
                    System.out.println("Server Stopped.");
        return;
            }

            throw new RuntimeException("Error accepting client connection", e);
        }

        WorkerRunnable workerRunnable = new WorkerRunnable(queue,clientSocket);

        this.threadPool.execute(workerRunnable);

    }
    this.threadPool.shutdown();

    System.out.println("Server Stopped.");
}

private synchronized boolean isStopped() {
    return this.isStopped;
}

public synchronized void stop() {
    this.isStopped = true;

    try {
        this.serverSocket.close();
    }

    catch (IOException e) {
        throw new RuntimeException("Error closing server", e);
    }
}

Here's what I don't understand: My while() loop that accepts for clients works as loong as isStopped is false.

When isStopped is set to true, my loop ends and then I shut down my thread pool, which is correct.

isStopped is set to true in onstop(){..............}....

Where should I call onstop()...?

Because in this moment I'm not using this method,I'm not calling it and that means that I'm not cleaning correctly my threads.

WorkerRunnable class:

    public class WorkerRunnable implements Runnable {
        public WorkerRunnable(BlockingQueue queue2, Socket clientSocket2) {
        // TODO Auto-generated constructor stub

            this.clientSocket2 = clientSocket2;

            this.queue2 = queue2;
        }

        public void run() {
            try {
                is = new ObjectInputStream(this.clientSocket2.getInputStream());

                try {
                    while (!stop) {
                        System.out.println("Suntem in interiorul buclei while:!");

                        v = (Coordinate) is.readObject();

                        queue2.put(v);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    is.close();

                    clientSocket2.close();
                }

                is.close();
                clientSocket2.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void stop() {
            this.stop = true;
        }
    }
}

Here I have the same issue. How should I clean and close up my sockets correctly?

Once you call shutdown() the thread pool will close off each thread once all the tasks are complete.

You should call onstop() from whatever code knows the pool should be shutdown. This depends on what the rest of your application does and why you would stop the pool before the application has finished.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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