在文档中,如果 BluetoothServerSocket.accept() 无论如何都会阻塞线程,为什么会在 while 循环中调用它?
In documentation, why is BluetoothServerSocket.accept() called in a while loop if it blocks the thread anyway?
在 android 文档中,以下代码出现在线程的 运行() 段中:
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
manageConnectedSocket(socket);
mmServerSocket.close();
break;
}
}
但是,accept() 方法会阻塞线程。因此,我不明白为什么需要 while() 循环,特别是因为在所有可能的情况下 while 循环在其第一个 运行.
中被破坏
有什么想法吗?
通常在接受和处理一个套接字后不会中断:您会无限期地循环接受套接字。
这是一个愚蠢的例子。
在 android 文档中,以下代码出现在线程的 运行() 段中:
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
socket = mmServerSocket.accept();
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
manageConnectedSocket(socket);
mmServerSocket.close();
break;
}
}
但是,accept() 方法会阻塞线程。因此,我不明白为什么需要 while() 循环,特别是因为在所有可能的情况下 while 循环在其第一个 运行.
中被破坏有什么想法吗?
通常在接受和处理一个套接字后不会中断:您会无限期地循环接受套接字。
这是一个愚蠢的例子。