Android,将数据推送到一组订阅线程的最佳方式?
在 Android (Java) 上,我在概念上遇到了提供以下内容的最佳方法的问题。我有一个线程从接口轮询数据包,并在可用时读取它们。然后,我有一组线程,它们应该在每个数据包到达时对它们进行操作(不同的线程有不同的操作)。
我想象的是一个线程应该能够“订阅”主线程上的数据包数据,并在每个数据包到达时获取其副本。我不介意(并且可能更喜欢)是否将数据复制到每个“订阅”线程。这样,我就不必担心线程同步某种共享数据的清理。
最好的方法是让主线程维护回调函数列表并在每个数据包上执行它们吗?但是,如果是这种情况,我如何确保函数在“从”线程上执行?有没有任何人都能想到的另一种/更好的方法来做到这一点?
On Android (Java), I am having trouble conceptually with the best way to provide the following. I have a thread which polls for packets from an interface, reading them in as they are available. Then, I have a set of threads which should operate on each packet as they arrive (different threads have different operations).
What I am imagining is a thread should be able to "subscribe" to the packet data on the main thread, and get a copy of each packet as it arrives. I do not mind (and might prefer) if the data is copied to each of the "subscribed" threads. That way, i do not have to worry about the threads synchronizing some sort of cleanup of shared data.
Is the best way to do this to have the main thread to maintain a list of callback functions and execute them on each packet? But, if that's the case, how do I ensure the functions are executed on the "slave" threads? Is there another/better way to do this that anyone can think of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个好的解决方案可能是为您感兴趣的通信事件注册广播接收器,然后让初始数据包处理器根据需要广播意图。请查看 http://justanapplication.wordpress.com,了解有关以下内容的详细系列帖子(8 个,并且还在增加)意图,以及开发指南中的以下两页。
http://developer.android.com/guide/topics/fundamentals.html
http://developer.android.com/guide/topics/intents/intents-filters.html
A good solution might be to register broadcast receivers for the events you're interested in communicating, and then have the initial data packet processor broadcast intents as appropriate. Take a look at http://justanapplication.wordpress.com for a detailed series of posts (8 and counting) on Intents, as well as the following two pages from the dev guide.
http://developer.android.com/guide/topics/fundamentals.html
http://developer.android.com/guide/topics/intents/intents-filters.html