复杂的android多线程问题,寻求一些指导
刚刚接触 stackoverflow,搜索很有帮助,但可惜现在是时候问一个问题了。
我正在尝试使用 android 2.2 单核手机进行一些研究。我已经实现了一种算法,可以进行大量计算并生成大量数据。必须处理这些数据,并在 40 毫秒的时间范围内将解决方案返回给客户端应用程序,然后使用来自客户端的新状态数据再次进行处理。此外,计算结果必须作为数据日志存储到 SD 卡中。因此,作为多线程和 android 的新手,我应该使用什么在我的应用程序中执行以下操作:(作为旁注,这款手机在处于研究模式时不打算用作手机,手机将处于飞行模式无线关闭,所有可以关闭的应用程序都将被关闭,并且一旦启动并运行就不需要 UI 显示或交互...)
- 需要处理通过串口上的 adb 传入的数据包,这些数据包是程序执行计算所需的状态数据。这些数据包每 40 毫秒到达一次,因此我计划利用它们的到达来触发处理的开始。
- 需要知道该算法是否花费了超过 40 毫秒的时间,如果是,则取消该算法,并在串行端口上发回一条超出其超时的消息。
- 计算结果需要通过tcp和adb通过串行连接发回
- 。计算中间数据需要记录到SD中。这可能是相当多的数据,大约为 140k,每 40 毫秒一次。
所以我很难将所有的碎片整合在一起。我无法理解单个核心将如何同时跟上所有这一切?
这是我的想法,请告诉我我是否走在正确的道路上。我不是要求你解决我的问题,只是关于如何打破这个野兽的任何建议:
- 所以我启动一个服务来处理来自客户端的 TCP 数据包
- 使用绑定到主工作线程的服务来处理写入SD 卡
因此假设这样的设置,我可以使算法部分具有一定的确定性,以便它在收到新的 tcp 数据包时始终运行,并抢占后台进行的 SD 写入吗?
啊...应该为我的第一个程序选择一些更简单的东西
谢谢。
New to stackoverflow, been very helpful searching, but alas the time has come to ask a question.
I am trying to use an android 2.2 single core phone to do some research. I have implemented an algorithm that does quite a few calculations and produces a lot of data. These data must be processed, and the solution presented back to a client app within a 40ms time frame, then process again with new state data coming from the client. Also, the result of the calculations must be stored to the SD card as a data log. So being new to multithreading and android both, what should I use to do the following in my app: (As a side note, this phone, when in research mode is not intended to be used as a phone, phone will be in airplane mode with wireless off, and all apps that can be turned off will be turned off, and there is no need for UI display or interaction once it is up and running...)
- need to process packets coming in over adb on serial port, these packets are state data that the program needs to perform its calcs on. These packets will be coming every 40ms, so I planned on using their arrival to trigger the start of the processing.
- need to know if the algorithm is taking longer than 40ms and cancel it if so and send a message back on the serial port that it overran.
- the calculation results need to be sent back over the serial connection via tcp and adb
- The calculation intermediate data need to be recorded to SD. This can be quite a lot of data, on order of 140k, every 40ms.
So I have had trouble getting all the pieces together. I can't get my head around how a single core is going to keep up with all this going on at once?
So here is my thought, please tell me if I am headed in the right path. I am not asking for you to solve my problem, only any advice on how to break this beast down:
- So i start a service to process the tcp packets coming in from the client
- Use a service bound to the main worker thread to handle writes to the SD card
So assuming this setup, can i make the algorithm part of this somewhat deterministic so that it always runs if it gets a new tcp packet, and preempts the SD write going on in the background?
Argh...should have picked something simpler for my first program
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,我认为你是对的,最好为你的第一个应用程序选择一些更容易的东西;)
但据我了解您想要做什么,我认为您不需要异步多处理。你得到一些数据想要处理它并传递结果。我认为 HandlerThread 正是您正在寻找的。它能够接收 消息
里面有数据。您将它们发送到 Handler 并在重写的handleMessage(Message m) 方法。因此,每次您收到消息时,您只需记录时间即可
看看最后一个是否比你的限制早。如果是,您可以直接抛出消息或整个队列,或者向串行端口发送一条消息来指示溢出。
这可以按照您在服务中的建议来实现。另一个 HandlerThread 可以使用 Thread.PRIORITY_BACKGROUND 启动,将所有内容写入标准差。
如果您将 Messenger 应用于处理程序,您甚至可以非常舒适地发送消息
Yes I think you are right, that it would be better to pick something easier for your first App ;)
But as far as I understand what you are trying to do, I don't think, that you need asynchronous multiprocessing. You get some data want to process it and pass a result. I think a HandlerThread is exactly what you are looking for. It is able to recieve Messages
with data inside. You send them to the Handler and process them in an overridden handleMessage(Message m) method. So everytime you recive a Message you could just log the Time
and see if the last one is older than your limit. If it is, you could just throw the Message or the whole queue, or send a Message to your serial-port inicating the overflow.
This could be implemented as you suggest in a Service. Another HandlerThread can be started with Thread.PRIORITY_BACKGROUND to write everything to SD.
You can send Messages even very compfortable if you apply a Messenger to the Handlers