我创建了AsynchronousServerSocketChannel,一切都很好,直到我想要关闭服务器套接字套接字。 我在客户端上创建了AsynchronousSocketChannel,它带有特殊的消息,调用函数来关闭服务器和所有套接字通道,服务器是关闭的,但是finnally block永远不会执行为什么?
检查代码:
public void openServerSocket(int sport) {
port = sport;
policyFile = createPolicy(new String[]{"*:"+port});
try {
//group = AsynchronousChannelGroup.withThreadPool(Executors.newSingleThreadExecutor());
//group.awaitTermination(1, TimeUnit.SECONDS);
server = AsynchronousServerSocketChannel.open();
server.bind(new InetSocketAddress(sport));
server.accept(null, this);
while(isOpen) {
//System.out.println("running server socket");
}
}catch(Exception ex) {
this.message = ex.getMessage();
System.out.println("Server: " + ex.getMessage());
}finally {
System.out.println("Finall serversocket closing!");
for(SocketHandler sh : sockets) {
sh.close();
}
if(server != null) {
try {
server.close();
server = null;
}catch(Exception ex2) {
this.message = ex2.getMessage();
}
}
sockets.clear();
sockets = null;
//this.message = "server socket closed";
System.out.println("server socket down");
}
}
@Override
public void completed(AsynchronousSocketChannel socket, Void attachment) {
System.out.println("new connection");
SocketHandler sh = new SocketHandler(socket, this);
server.accept(null, this);
}
@Override
public void failed(Throwable exc, Void attachment) {
System.out.println("Server Failed accept:" + exc.getMessage());
}
public void closeServerSocket() {
System.out.println("Closing server socket! " + server);
this.isOpen = false;
}
因此函数closeServerSocket被调用,我将false设置为变量isOpen,它保持循环,但似乎当我将is设置为isOpen var时,仍然是循环并且最终永远不会被执行?
答案 0 :(得分:1)
这看起来像是几个线程之间的通信问题。从代码中我猜你的while(isOpen)
循环运行在与你的类的closeServerSocket()
方法不同的线程中。要验证这一点,请在打印报表中添加Thread.currentThread()
。
如果是这种情况,您可以通过将isOpen
声明为volatile
来解决问题。如果没有volatile
,则无法保证,如果和任何其他线程将看到写入的值。使用volatile
可以保证(我对happens-before的解释)线程访问变量以后(根据挂钟时间)将看到更改的值。
而不是volatile
您可能更喜欢AtomicBoolean
。