Android:后台线程可能会阻塞,直到 UI 线程完成操作?
后台线程是否可以将消息排队到主 UI 线程的处理程序并阻塞,直到该消息得到处理?
其上下文是,我希望我的远程服务从其主 UI 线程(而不是从中接收 IPC 请求的线程池线程)为每个已发布的操作提供服务。
Is it possible for a background thread to enqueue a message to the main UI thread's handler and block until that message has been serviced?
The context for this is that I would like my remote service to service each published operation off its main UI thread, instead of the threadpool thread from which it received the IPC request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以满足您的需要。它使用
notify()
和wait()
与已知对象来使该方法本质上同步。run()
内部的任何内容都将在 UI 线程上运行,并在完成后将控制权返回给doSomething()
。这当然会让调用线程进入睡眠状态。This should do what you need. It uses
notify()
andwait()
with a known object to make this method synchronous in nature. Anything inside ofrun()
will run on the UI thread and will return control todoSomething()
once finished. This will of course put the calling thread to sleep.