接受不会响应中断,除非它是从 ServerSocketChannel 创建。使用 ServerSocket 构造函数将忽略中断,我还没有找到重新配置它的方法。However, a call to accept will not respond to an interrupt unless it is created from a ServerSocketChannel. A server created with a ServerSocket constructor will ignore interrupts, and I haven't found a way to reconfigure this.如果你不能改变创建服务器的代码,安排另一个线程在服务器套接字上调用 close 。无论用于创建服务器套接字的方法如何,这也会在 accept 中阻塞线程中引发异常。If you can't change the code that creates the server, arrange for another thread to call close on the server socket. This will also raise an exception in thread blocked on accept, regardless of the method used to create the server socket.使用SSL时,这是一个非常大的痛苦。 JSSE套接字不是从 InterruptibleChannel 创建的,并且不会响应线程上的简单中断。This turns out to be a really big pain when using SSL. A JSSE socket is not created from an InterruptibleChannel, and won't respond to a simple interrupt on the thread.我刚才注意到问题是在没有通知客户端的情况下无法关闭服务器。成功中断套接字会导致其关闭。I just noticed that the question says that the server can't be closed without notifying the client. Successfully interrupting a socket results in its closure.在调用接受这应该不是问题,如果服务器套接字在accept中被阻止,则客户端未连接。这应该只是 Socket 实例的问题,代表当前连接。On a call to accept this shouldn't be a problem, since the client is not connected if the server socket is blocked in accept. That should only be an issue for Socket instances, that represent current connections.如果不满足通知要求,可能需要在非阻塞模式下使用NIO的 ServerSocketChannel 进行返工。If that doesn't satisfy the notification requirements, a rework to use NIO's ServerSocketChannel in non-blocking mode may be necessary. 这篇关于如何解除阻塞在ServerSocket.accept()上阻塞的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!