停止ServerSocket accept()循环线程

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

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

你将如何实现stop()方法? 请记住,stop()可以在doSomething()内部调用,在start()启动的同一个辅助线程中。 您可以更改任何所需内容:如果需要,可以在线程内创建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; } } 

我会做的

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

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

我会让running不稳定。

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