在下面的块中,我知道这个主线程正在阻塞,因此没有侦听来自其他线程的中断,但这是否意味着它实际上挂在ServerSocket.accept((行上?
ServerSocket serverSocket = new ServerSocket(8080);
while(true){ // Block until accept
Socket acceptedSocket = serverSocket.accept(); // Give accepted socket OR NULL SOCKET to acceptedSocket
handle(acceptedSocket);
serverSocket.close();
}
我这么问是因为我不明白它会挂在哪里接受(ServerSocket.java(:
public Socket accept() throws IOException {
if (isClosed()) // No its open
throw new SocketException("Socket is closed");
if (!isBound()) // No its bound to 8080
throw new SocketException("Socket is not bound yet");
Socket s = new Socket((SocketImpl) null); // Create a null socket
implAccept(s); // Here???
return s;
}
ServerSocket.java再次出现:
protected final void implAccept(Socket s) throws IOException {
SocketImpl si = null;
try {
if (s.impl == null)
s.setImpl();
else {
s.impl.reset();
}
si = s.impl;
s.impl = null;
si.address = new InetAddress();
si.fd = new FileDescriptor();
getImpl().accept(si); // I'm thinking its here but when I look at this accept, thats an abstract method and don't know where to dig deeper.
取决于操作系统,但在Linux上,accept((是一个内核调用。最直接的Java实现是使用内核调用。
因此,这个过程是"在内核中等待",粗略地说。