如何扩展现有的后台线程解决方案?
我正在使用 Eclipse 开发一个绘制蓝牙数据的 Android 应用程序。
我正在使用开源代码,它有一个现有的解决方案,我想扩展而不是替换来解决如上所述的开发问题。
开源代码有一个非常漂亮且可靠的后台线程,即使我切换到新活动,它也会不断将蓝牙数据记录到 logcat 中。
目前我有一个我担心的解决方案:我只是利用写入 logcat 的后台线程方法来调用绘图活动中的静态plotData() 方法。结果看起来不错。我得到了一个很好的情节。它实时剪辑。看起来像示波器。
但我收到了关于使用现有后台线程和静态方法来绘制蓝牙日期的负面反馈。有人建议我使用新线程,或者添加处理程序,或者使用 Async Task 或 AIDL 来解决我的问题。
我查看了所有这些解决方案但没有成功。似乎没有什么比我的静态plotData() 方法更有效。也就是说,现有的后台线程调用我的静态plotData() 方法,这会产生看起来很棒的实时绘图。
但我仍然担心负面反馈。我只是想扩展我现有的后台线程解决方案,通过调用静态方法来绘制数据来完成该解决方案。
采用这种方法可能会遇到哪些问题?线程安全?僵局?我不知道。
为什么人们总是建议我创建一个新的线程、处理程序、异步任务或服务来解决我的问题,而扩展我现有的线程来调用静态方法似乎工作得很好?
有什么建议吗?扩展现有线程以使用静态方法实时绘制数据有哪些问题?
I am using Eclipse to develop an Android application that plots Bluetooth data.
I am using open source code, which has an existing solution that I want to extend and not replace to solve my development problem as stated above.
The open source code has a very nice and solid background thread that among other things continually logs BluetoothData to logcat even when I switch to a new activity.
Currently I have a solution which I am concerned about: I simply leverage a background thread method that writes to logcat to call a static plotData() method in my Plotting Activity. The result seems good. I get a nice plot. It clips along in real-time. Looks like an oscilloscope.
But I have received negative feedback about using the existing background thread coupled with a static method to plot the BluetoothDate. It has been suggested that I use a new thread, or add a handler, or use Async Task, or AIDL to solve my problem.
I have looked at all these solutions without success. Nothing seems to work like my static plotData() method. That is to say the existing background thread calls my static plotData() method which results in a real-time plot that looks great.
But I am still concerned about the negative feedback. I simply want to extend my existing background thread solution which I have done by having it call a static method to plot the data.
What are the problems I might face with this approach? Thread safety? Deadlock? I don't know.
Why do people keep suggesting that I create a new thread, handler, Async Task, or Service to solve my problem when extending my existing thread to call a static method seems to work just fine?
Any suggestions? What are the problems with extending the existing thread to use a static method to plot the data in real-time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
任何说为此应该使用 AIDL 的人都是白痴,不应该被听取。 :) 还有人说,如果您不想在用户没有查看您的活动时运行后台线程,那么您需要一个服务。
我不确定“写入 logcat 以调用静态plotData()”是什么意思。您应该仅为测试而写入 logcat。写入 logcat 不会导致调用任何 Java 方法。
如果您在 Activity 上调用静态plotData() 方法,则需要极其小心:首先,因为很难弄清楚应该从那里调用哪个 Activity 实例(它可能会用户可以随时完成它,或者在配置更改时重新创建为新实例等);其次,因为您无法从后台线程触摸应用程序的 UI/视图层次结构,而不冒破坏其状态的风险(因为视图层次结构是单线程的)。
对于这种事情所做的一般模型是让后台线程做一些工作,生成下一个要显示的数据。完成工作后,您将向主线程发送一条消息以使其显示新数据。 AsyncTask 是执行此操作的一种简单方法,它负责底层消息发送和线程处理。您也可以自己实现这一点,在某个时刻,拥有一个处理程序,您可以在该处理程序上发布 Runnable 或向该处理程序发送一条消息,一旦在 UI 线程上执行,就会更新您的视图状态。
(当然,如果您使用的是 SurfaceView,那么重点是允许在主 UI 循环之外对其进行绘制,因此您的后台线程可以根据需要直接在其上进行绘制。基本上,这就像编写游戏一样。 )
Anyone who says that you should use AIDL for this is a loon who should not be listened to. :) Also someone saying you need a Service if you don't want to have your background thread running when the user is not viewing your activity.
I'm not sure what you mean by "writes to logcat to call a static plotData()." You should write to logcat only for testing. Writing to logcat doesn't cause a call to any Java method.
If you are calling a static plotData() method on your Activity, you need to be extremely careful with this: first because it is difficult to figure out what activity instance should be called from there (it may go away at any time from the user finishing it, or be recreated as a new instance when the configuration changes, etc); and second because you can't touch your app's UI/view hierarchy from a background thread without risking that you corrupt its state (since the view hierarchy is single threaded).
The general model one does for this kind of thing is to have a background thread doing some work, generating the next data to display. Once it is done with the work you send a message to the main thread to have it display the new data. AsyncTask can be a simple way to do this, it takes care of the underlying message sending and threading. You can also implement this yourself, at some point having a Handler that you post a Runnable on or send a Message to that once executed on the UI thread will update your view state.
(Of course if you are using a SurfaceView, the whole point of that is to allow drawing to it outside of the main UI loop, so your background thread could just draw directly on to it as needed. Basically that is like writing a game.)