简体   繁体   English

Android / Java关于线程的决策

[英]Android/Java decisions about threading

Okay, so I after playing around with the Android SDK a little I have decided to develop a full application. 好的,所以在使用了Android SDK之后,我决定开发一个完整的应用程序。 Not necessarily to release on the store (I don't really have the tools at my disposal to do proper QA and so wouldn't feel right; I could only test on my one personal device) it is mostly just a personal project for fun and learning. 不一定要在商店中发布(我真的没有可用的工具来进行适当的质量检查,因此感觉不对;我只能在自己的个人设备上进行测试),这基本上只是一个有趣的个人项目和学习。 In the end, I decided on a camera application because I use my camera fairly often so it is the kind of application I would end up actually using if I made it and also because I am quite excited by the features of the new Camera2 API in Android L and want to give them a try. 最后,我决定使用相机应用程序,因为我经常使用相机,所以如果我制作了相机,那将是我最终会使用的那种应用程序,并且因为我对相机中新的Camera2 API的功能感到非常兴奋Android L,想要尝试一下。

Anyway, I have been thinking on the design/architecture of the application for a while and I have come across a potential problem that I cannot really resolve myself as I am not only not really familiar with GUI programming of this nature but also not 100% on Java and the Android SDK. 无论如何,我一直在思考应用程序的设计/架构,并且遇到了一个潜在的问题,我无法真正解决自己,因为我不仅不真正熟悉这种性质的GUI编程,而且也不是100%在Java和Android SDK上。 They both sit well outside the purview of my dayjob. 他们俩都坐在我日托工作的职权范围之外。

Now I know enough that I really need to keep my UI thread separate from everything else, but what I am unsure about is first of all, what I should devote extra threads to (I was thinking one to handle image capture and storage, and another to handle the settings? perhaps that is over doing it) and also whether to have background threads which the UI thread communicates with through Events or simply to spawn threads as and when they are needed. 现在我已经知道我确实需要将UI线程与其他所有线程分开,但是我不确定的是首先是什么,我应该投入额外的线程(我当时想到的是一个用于处理图像捕获和存储的线程,另一个用于来处理设置(也许是这样做了),以及是否拥有UI线程通过事件与之通信的后台线程,还是仅在需要时生成线程。 Perhaps a single intermediate background thread which arbitrates between the UI thread and any background processing that needs to be done? 也许有一个在UI线程和任何需要完成的后台处理之间进行仲裁的中间后台线程? What I mean to ask is, how do I decide these things? 我要问的是,我如何决定这些事情? Are there general/best practices? 是否有一般/最佳做法?

I'm really asking because I want to get this done properly and plan out the application thoroughly beforehand since in the past I have found that if I just dive right in I get confused very easily and end up losing faith and dropping the project. 我之所以这么问,是因为我想妥善完成这项工作,并事先对应用程序进行彻底规划,因为过去我发现,如果我只是潜入水中,我会很容易感到困惑,最终会失去信心并放弃该项目。 I hope somebody can help. 希望有人能帮忙。 It's not a question that I have found easy to phrase in such a way that Google would provide satisfactory results. 我发现用Google可以提供令人满意的结果的方式来表达这句话并不是一个容易的问题。

I think you might consider thinking about it this way: 我认为您可以考虑这样考虑:

The UI thread is where most stuff runs. UI线程是大多数东西运行的地方。 It is also, occasionally, called the "main" thread, because it is, uhhh... the main thread. 有时也称为“主”线程,因为它是主线程。

Unless your program is wildly complex, you probably don't need to worry about an architecture for threads. 除非您的程序非常复杂,否则您可能不必担心线程的体系结构。 All you need to worry about is getting slow stuff off the UI thread. 您需要担心的是 UI线程获取缓慢的东西。 That's it. 而已。 You don't care where they go, just not the UI thread. 您不在乎它们的去向,只是不在乎UI线程。

If you buy that, there are some nifty choices: 如果您购买该产品,则有一些不错的选择:

  1. Intent Service : If, a bit metaphorically, the methods you need to run in "background" have void return types, an intent service is the perfect solution. 意图服务 :如果有点隐喻地表示,您需要在“后台”中运行的方法具有无效的返回类型,则意图服务是理想的解决方案。 You communicate with it using Intents and, since it has only one thread, tasks you submit to that thread are run in order. 您使用Intents与之通信,并且由于它只有一个线程,因此提交给该线程的任务将按顺序运行。
  2. AsyncTask : These are fraught with problems but people get a lot of programming done with them. AsyncTask :这些问题很多,但是人们可以通过它们完成很多编程工作。 They run in a pool of around 6 threads, but (surprise!) run in order anyway. 它们在大约6个线程的池中运行,但是(惊奇!)仍然按顺序运行。
  3. Your own Java Executor : Use this if you need behavior similar an AsyncTask, but with more control. 您自己的Java执行器 :如果您需要类似AsyncTask的行为,但具有更多控制权,请使用此函数。
  4. New thread per task : Just don't do this. 每个任务都有新线程 :只是不要这样做。 Really. 真。

In any of these cases, though, you don't really care on which threads your tasks are running. 但是,无论在哪种情况下,您都不在乎任务在哪个线程上运行。 You make a policy about the number of threads that makes sense on the target device and then run your slow stuff on one of them. 您制定了有关在目标设备上有意义的线程数量的策略,然后在其中一个上运行速度较慢的任务。

...and "slow" should include all I/O: databases, file system, network, etc., as well as major computation. ...“慢”应该包括所有I / O:数据库,文件系统,网络等,以及主要的计算。

You want to keep things as simple as possible. 您想使事情尽可能简单。 You don't need a thread to set settings, and you don't need an intermediary thread. 您不需要线程来设置设置,也不需要中间线程。 These things are only going to create unnecessary complication. 这些事情只会造成不必要的复杂性。 You only need to take long running operations off the UI thread. 您只需要在UI线程上执行长时间运行的操作即可。

Take a look at the structures the Android framework provides to make multi-threading easier first. 看一下Android框架提供的使多线程首先变得更容易的结构。 This will help simplify your application. 这将有助于简化您的应用程序。 These would include IntentService and AsyncTask . 这些将包括IntentServiceAsyncTask If those won't meet your needs take a look at the java.util.concurrent package. 如果这些不能满足您的需求,请查看java.util.concurrent包。 Using a raw Thread is often not advisable. 通常不建议使用原始线程。

Another library that could come in handy is an Event Bus like Otto . 可以派上用场的另一个库是像Otto这样Event Bus This will prevent memory leaks that can occur with using AsyncTask. 这将防止使用AsyncTask可能发生的内存泄漏。

You dont need to worry about threads when it comes to image capture. 在图像捕获方面,您无需担心线程。 When you want to take a picture you do so by opening the Camera App on the device. 当您要拍照时,可以通过在设备上打开Camera App来进行拍照。 Here is a good way of doing that. 是一个很好的方法。

Android SDK provides Asynctask which does things for you in the background thread, like a network call and updates the UI once its done. Android SDK提供了Asynctask ,可在后台线程中为您执行操作,例如网络调用,并在完成后更新UI。

You can also use an IntentService , which runs even if you exit the app and does things like upload/download without the user having to worry about it. 您还可以使用IntentService ,即使您退出应用程序,它也会运行,并且执行诸如上载/下载之类的操作,而无需用户担心。 Loaders are useful when the underlying data often changes and you need to update UI quickly (searching contacts in autocomplete text for example). 当基础数据经常更改并且您需要快速更新UI(例如,以自动完成文本搜索联系人)时, 加载程序很有用。

http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html

Refer to this tutorial, its a good start. 请参考本教程,这是一个好的开始。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM