停止ServerSocket accept()循环线程

时间:2011-04-14 08:17:48

标签: java multithreading sockets tcp serversocket

我正在实现一个非常基本的API来更好地控制ServerSocket和Sockets,但是由于我缺乏线程知识,我处理的是一个非常奇怪的问题。让我解释一下。

在我的类SocketStreamReceiver中,我使用辅助线程来监听ServerSocket#accept()的新套接字。有两种方法:start()和stop()客户端可以用来启动(创建一个线程并开始用accept()监听)并停止(关闭ServerSocket并销毁线程)我的SocketStreamReceiver。

你将如何实现stop()方法?请记住,在start()启动的同一个辅助线程中,doSomething()内部可以调用stop()。你可以改变你想要的任何东西:如果你愿意,你可以在线程内创建ServerSocket,就在while(运行)之前。

public class SocketStreamReceiver{
    ...
    private Thread thread;
    private ServerSocket server;
    private boolean running;
    ...

    public void start () throws IOException{
        if (thread != null) return;

        server = new ServerSocket (port);
        thread = new Thread (new Runnable (){
            @Override
            public void run (){
                try{
                    while (running){
                        Socket socket = server.accept ();
                        doSomething (socket);
                    }
                }catch (SocketException e){
                    ...
                }catch (IOException e){
                    ...
                }
            }
        }, "SocketStreamReceiver");
        thread.start ();
    }

    public void stop () throws IOException{
        if (thread == null) return;

        //code...

        thread = null;
    }
}

感谢。

编辑 - 解决方案:

public class SocketStreamReceiver{
    private Thread thread;
    private ServerSocket server;
    private volatile boolean running;
    ...

    public synchronized void start () throws IOException{
        if (thread != null) throw new IllegalStateException ("The receiver is already started.");

        server = new ServerSocket (port);
        thread = new Thread (new Runnable (){
            @Override
            public void run (){
                try{
                    running = true;
                    while (running){
                        doSomething (server.accept ());
                        ...
                    }
                }catch (SocketException e){
                    ...
                }catch (IOException e){
                    ...
                }
            }
        }, "SocketStreamReceiver");
        thread.start ();
    }

    public synchronized void stop (){
        if (thread == null) return;

        running = false;
        try{
            if (server != null){
                server.close ();
            }
        }catch (IOException e){}

        thread = null;
    }
}

1 个答案:

答案 0 :(得分:2)

我会做的

public void stop() {
    running = false;
    try{
        if (server != null) server.close ();
    } catch (IOException ignored){
    }
}

看起来你甚至不需要运行标志。但是,我会在您的服务器接受代码中使用它来确定是否预期异常。即在运行时== false忽略所有异常。

我会让running变得不稳定。

如果你可以从不同的线程运行它们,我会使start()/ stop()同步。