[英]Android/Sockets - How to send information from main UI thread to socket thread?
I've been reading through forums for the past day and I can't seem to find an answer that would help solve my problem. 在过去的一天中,我一直在阅读论坛,但似乎找不到能够解决我的问题的答案。
I am trying to create an android socket/game client which: 我正在尝试创建一个android套接字/游戏客户端:
I am having a lot of difficulty with implementing #3. 我在实施#3时遇到很多困难。 The socket that is initially connected to the server is in its own thread, which I want to keep open indefinitely. 最初连接到服务器的套接字在其自己的线程中,我想无限期保持打开状态。 What is the best way to get information from the main UI thread so that I can use that thread and socket to send information to the server? 从主UI线程获取信息的最佳方法是什么,以便我可以使用该线程和套接字将信息发送到服务器?
I see a lot of posts about returning information from a background thread to the UI thread but not the other way around. 我看到了很多关于将信息从后台线程返回到UI线程的文章,但并非相反。 Is there a standard way of doing this? 有这样做的标准方法吗?
Thanks! 谢谢!
I've found a solution for this where I used a BlockingQueue that is shared between the UI thread and the socket thread. 我找到了一个解决方案,其中使用了UI线程和套接字线程之间共享的BlockingQueue。 The UI thread uses queue.put() when it wants to place information that is to be emitted to the server, and the socket thread waits on queue.take() to get the information and send it to the server. UI线程在要放置要发送到服务器的信息时会使用queue.put(),并且套接字线程在queue.take()上等待获取信息并将其发送到服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.