Netty
네트워킹 라이브러리를 찢어 내고 있습니다. 기본 NIO 네트워킹 코드를 작성하는 법을 배우려하고 있는데, 나에게 맞지 않는 무언가를 만났을 때, for-loop 안에 아무 것도없는 코드가 있습니다. 코드는 다음과 같습니다. :Java for (;;) 루프?
for (;;) {
SocketChannel acceptedSocket = channel.socket.accept();
if (acceptedSocket == null) {
break;
}
registerAcceptedChannel(channel, acceptedSocket, thread);
}
나는 즉시 here에 위치한 루프의 문서 튜토리얼을 확인하고이 사항에 관련된 아무것도 찾을 수 없습니다.
// accept connections in a for loop until no new connection is ready
그러나 작동, 또는 왜, 그냥이 무엇을하고 있는지 말한다 방법이 정말 나에게 설명하지 않습니다
직접 코드 위의 해설은 다음을 말한다. 당신은 모든 방법을해야하는 경우
는 여기있다 :
@Override
protected void process(Selector selector) {
Set<SelectionKey> selectedKeys = selector.selectedKeys();
if (selectedKeys.isEmpty()) {
return;
}
for (Iterator<SelectionKey> i = selectedKeys.iterator(); i.hasNext();) {
SelectionKey k = i.next();
i.remove();
NioServerSocketChannel channel = (NioServerSocketChannel) k.attachment();
try {
// accept connections in a for loop until no new connection is ready
for (;;) {
SocketChannel acceptedSocket = channel.socket.accept();
if (acceptedSocket == null) {
break;
}
registerAcceptedChannel(channel, acceptedSocket, thread);
}
} catch (CancelledKeyException e) {
// Raised by accept() when the server socket was closed.
k.cancel();
channel.close();
} catch (SocketTimeoutException e) {
// Thrown every second to get ClosedChannelException
// raised.
} catch (ClosedChannelException e) {
// Closed as requested.
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn(
"Failed to accept a connection.", t);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// Ignore
}
}
}
}
그것은 무한 루프입니다. – squiguy
for 루프의 모든 부분은 선택 사항입니다. 확인해야 할 조건이 없으며 실행 단계가 없기 때문에 이는 대다수의 루프로 볼 수 있습니다. – Makoto
...'break;'와 함께 종료됩니다 –