gpt4 book ai didi

android - 线程是否停止执行并继续?这只是令人困惑

转载 作者:行者123 更新时间:2023-11-30 02:47:15 25 4
gpt4 key购买 nike

我在代码中添加了日志只是为了了解它的执行顺序和时间,但到目前为止它只是令人困惑!

当我执行这段代码并启动服务器时,我得到了这些输出


"启动服务器", "ServerThread"

"监听", "服务器线程"

“连接到达”,“ServerThread”


然后停在那里,当我连接到服务器时,我只得到


“已接受连接”,“ServerThread”


就好像挂着一样

socket = serverSocket.accept();

难道不应该因为某种异常而退出尝试并再次打印整个内容吗?

class ServerThread implements Runnable {

public void run() {
Socket socket = null;
try {
Log.e("start server", "ServerThread");
serverSocket = new ServerSocket(SERVERPORT);
} catch (IOException e) {
e.printStackTrace();
Log.e("IO error at starting server", "ServerThread");
} catch (Exception e) {
Log.e("Exc at starting server", "ServerThread");
}
while (!Thread.currentThread().isInterrupted()) {
Log.e("listening", "ServerThread");
try {
Log.e("connection arrived", "ServerThread");
socket = serverSocket.accept();
Log.e("connection accepted", "ServerThread");
CommunicationThread commThread = new CommunicationThread(
socket);
new Thread(commThread).start();

} catch (IOException e) {
e.printStackTrace();
Log.e("IO error at accept", "ServerThread");
} catch (Exception e) {
Log.e("Exc at accept", "ServerThread");
}
}

}
}

最佳答案

这是一个阻塞方法,它会阻塞直到你收到一个连接。

查看文档:http://docs.oracle.com/javase/7/docs/api/java/net/ServerSocket.html#accept()

关于android - 线程是否停止执行并继续?这只是令人困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24828051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com