简体   繁体   English

我整个Android应用程序的一个线程池

[英]one Thread Pool for my whole android application

like - network operation and bitmap manipulating an image loading and other kinds of work can I create a single TheadPoolExecuter for my whole application and execute on it. 像-网络操作和位图操纵图像加载以及其他类型的工作,我可以为整个应用程序创建一个TheadPoolExecuter并在其上执行。

if the answer is no -> why? 如果答案是否定的->为什么? and how to create thread pool for every single operation? 以及如何为每个操作创建thread pool

or if yes -> is performance problem occurs? 还是如果是->是否出现性能问题?

thanks in advance. 提前致谢。

Talking teorically i think you can do that and according to oracle documentation should be improve your performance: 从技术上讲,我认为您可以做到,根据oracle文档,应该可以提高性能:

Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and managing the resources, including threads, consumed when executing a collection of tasks. 线程池解决了两个不同的问题:由于减少了每个任务的调用开销,它们通常在执行大量异步任务时提供改进的性能,并且它们提供了一种绑定和管理资源(包括线程)的方法,这些资源在执行集合时消耗掉了。任务。 Each ThreadPoolExecutor also maintains some basic statistics, such as the number of completed tasks. 每个ThreadPoolExecutor还维护一些基本统计信息,例如已完成任务的数量。

Both of approach have advantages and disadvantages. 两种方法都有优点和缺点。

In case of single thread pool (singleton implementation, I suppose): 在单线程池的情况下(我想是单例实现):
➕ you have one entry point to submit background task ➕您有一个提交点来提交后台任务
➕ it easily to implement and control life cycle easily易于实现和控制生命周期
➖ if you have a lot of different quick tasks and some long running task, long running tasks may hold all thread in limited pool while user wait some quick action in UI ➖如果您有许多不同的快速任务和一些长时间运行的任务,则长时间运行的任务可能会将所有线程保存在有限的池中,而用户在UI中等待一些快速操作

Different thread pools (one pool for one type of task): 不同的线程池(一种任务类型的一个池):
➕ thread pool of long-running tasks can accumulate task while quick task can be executed in their own thread pool in-depend long长时间运行的任务的线程池可以累积任务,而快速任务可以在其自己的线程池中独立执行
➕ you know everything about tasks in your application - you can fine-tune pool size for every type of task, setup threads priority, initial stack size etc. with thread factory ➕您了解有关应用程序中任务的所有信息-您可以使用线程工厂微调每种任务的池大小,设置线程优先级,初始堆栈大小等
➕ if you define thread group and thread name, it can help you in debug ➕如果定义线程组和线程名称,则可以帮助您进行调试
➖ have different thread pools involve to hard control their life cycle ➖有不同的线程池涉及硬性控制其生命周期
➖ this implementation will not give a lot of benefits in poor separation by tasks classes implementation这种实现在按任务类别进行差的分离方面不会带来很多好处

Any case, you need some compromise and an assessment of the advantages 无论如何,您都需要做出一些妥协并评估其优势

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

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