清理线程池的问题

时间:2011-06-10 15:24:08

标签: java multithreading sockets

我在Java中有一个多线程服务器,我为它创建了一个线程池。 现在一切顺利,我的服务器接受并从连接到它的客户端读取数据,但我不知道如何在连接关闭后清理套接字。

所以这是我的代码:

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

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

ThreadPooledServer类:

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

以下是我不理解的内容:我的while()循环接受客户端工作为isStopped的错误。

isStopped设置为true时,我的循环结束,然后我关闭了我的线程池,这是正确的。

isStopped在onstop(){..............} ....

中设置为true

我应该在哪里打电话给onstop()......?

因为在这一刻我没有使用这种方法,我没有调用它,这意味着我没有正确清理我的线程。

WorkerRunnable类:

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

我有同样的问题。我应该如何正确清洁和关闭插座?

1 个答案:

答案 0 :(得分:0)

一旦你调用shutdown(),一旦所有任务完成,线程池将关闭每个线程。

您应该从任何知道池应该关闭的代码中调用onstop()。这取决于应用程序的其余部分以及在应用程序完成之前停止池的原因。