gpt4 book ai didi

java - 如何暂停线程一段时间,然后在用户交互后显示UI,继续线程执行

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:01 26 4
gpt4 key购买 nike

我已经启动了线程,在该线程中我尝试连接到服务器,收到响应后,我必须使用事件监听器更新 UI(通过接口(interface)实现)。这里,收到响应后,一旦用户单击“确定”,我需要显示弹出对话框,需要继续线程并完成其他过程。

 class ConnectionThread extends Thread {
ConnectionThread() {
this.setName("ConnectionThread");
}

@Override
public void run() {
// Need to pause the thread for sometime, Need to do the functionality here.
((Activity)mContext).runOnUiThread(new Runnable() {
public void run() {
// custom dialog
showAlertDialog();
// start the thread functionality again from that position.
}
});

}

我尝试过 wait() 概念并加入,但没有达到预期的效果。任何帮助表示赞赏。

最佳答案

您可以使用countdownlatch

class ConnectionThread extends Thread {
CountDownLatch countDownLatch = new CountDownLatch(1);
public ConnectionThread() {
this.setName("ConnectionThread");
}

@Override
public void run() {
try {
sleep(2000);
runOnUiThread(new Runnable() {
@Override
public void run() {
//update ui then
countDownLatch.countDown();
}
});
countDownLatch.await();
//start process again
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

关于java - 如何暂停线程一段时间,然后在用户交互后显示UI,继续线程执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41446285/

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