CachedThreadPool 与 FixedThreadPool
CachedThreadPool vs FixedThreadPool
我想知道在这种特定情况下该使用 CachedThreadPool
还是 FixedThreadPool
。
当用户登录应用程序时。将获得大约 10 个地址的地址列表。我需要执行以下操作:
- 将地址转换为我调用的经纬度googleapi
- 也借助google的api
获取上述获取的经纬度与用户当前位置的距离
所以我创建了一个 class GetDistance
来实现 Runnable
- 在这个 class 我首先调用 google api并解析resposne得到各自的纬度和经度,然后调用和解析另一个google api的结果得到行驶距离。
private void getDistanceOfAllAddresses(List<Items> itemsList) {
ExecutorService exService = newCachedThreadPool(); //Executors.newFixedThreadPool(3);
for(int i =0; i<itemsList.size(); i++) {
exService.submit(new GetDistance(i,usersCurrentLocation));
}
exService.shutdown();
}
我已经尝试过 CachedThreadPool
和 FixedThreadPool
- 花费的时间几乎相同我也赞成 CachedThreadPool 因为它被推荐用于小型操作但我担心的是 - 让我们假设 CachedThreadPool
创建 10 个线程(最坏情况)来完成该过程(10 个项目)如果我的应用程序在低端设备上 运行 会不会有问题?由于创建的线程数也会影响设备的 RAM。
想知道你对此的想法和看法。哪个更好用?.
谢谢
选择 newCachedThreadPool
更适合这种情况,因为您的任务很小并且 I/O(网络)受限。这意味着您应该创建大于处理器内核数量的线程(通常是 1.5 倍至 2 倍)以获得最佳输出,但我猜这里 newCachedThreadPool
会自行管理。因此,与 newFixedThreadPool
相比,newCachedThreadPool
的开销会更少,并且对您的情况有所帮助。
如果您有 CPU 繁重的任务,那么 newFixedThreadPool
可能是更好的选择。
更新
A list of addresses will be obtained about 10 addresses.
如果你总是只需要10个地址,那没关系,用newCachedThreadPool
吧。但是,如果您认为地址数量可以增加,那么使用 newFixedThreadPool
线程数 <= 1.5 倍到 2 倍可用内核数。
来自 Java 文档:
Creates a thread pool that reuses a
fixed number of threads operating off
a shared unbounded queue. At any
point, at most nThreads threads will
be active processing tasks. If
additional tasks are submitted when
all threads are active, they will wait
in the queue until a thread is
available. If any thread terminates
due to a failure during execution
prior to shutdown, a new one will take
its place if needed to execute
subsequent tasks. The threads in the
pool will exist until it is explicitly
shutdown.
Creates a thread pool that creates new
threads as needed, but will reuse
previously constructed threads when
they are available. These pools will
typically improve the performance of
programs that execute many short-lived
asynchronous tasks. Calls to execute
will reuse previously constructed
threads if available. If no existing
thread is available, a new thread will
be created and added to the pool.
Threads that have not been used for
sixty seconds are terminated and
removed from the cache. Thus, a pool
that remains idle for long enough will
not consume any resources. Note that
pools with similar properties but
different details (for example,
timeout parameters) may be created
using ThreadPoolExecutor constructors.
我想知道在这种特定情况下该使用 CachedThreadPool
还是 FixedThreadPool
。
当用户登录应用程序时。将获得大约 10 个地址的地址列表。我需要执行以下操作:
- 将地址转换为我调用的经纬度googleapi
- 也借助google的api 获取上述获取的经纬度与用户当前位置的距离
所以我创建了一个 class GetDistance
来实现 Runnable
- 在这个 class 我首先调用 google api并解析resposne得到各自的纬度和经度,然后调用和解析另一个google api的结果得到行驶距离。
private void getDistanceOfAllAddresses(List<Items> itemsList) {
ExecutorService exService = newCachedThreadPool(); //Executors.newFixedThreadPool(3);
for(int i =0; i<itemsList.size(); i++) {
exService.submit(new GetDistance(i,usersCurrentLocation));
}
exService.shutdown();
}
我已经尝试过 CachedThreadPool
和 FixedThreadPool
- 花费的时间几乎相同我也赞成 CachedThreadPool 因为它被推荐用于小型操作但我担心的是 - 让我们假设 CachedThreadPool
创建 10 个线程(最坏情况)来完成该过程(10 个项目)如果我的应用程序在低端设备上 运行 会不会有问题?由于创建的线程数也会影响设备的 RAM。
想知道你对此的想法和看法。哪个更好用?.
谢谢
选择 newCachedThreadPool
更适合这种情况,因为您的任务很小并且 I/O(网络)受限。这意味着您应该创建大于处理器内核数量的线程(通常是 1.5 倍至 2 倍)以获得最佳输出,但我猜这里 newCachedThreadPool
会自行管理。因此,与 newFixedThreadPool
相比,newCachedThreadPool
的开销会更少,并且对您的情况有所帮助。
如果您有 CPU 繁重的任务,那么 newFixedThreadPool
可能是更好的选择。
更新
A list of addresses will be obtained about 10 addresses.
如果你总是只需要10个地址,那没关系,用newCachedThreadPool
吧。但是,如果您认为地址数量可以增加,那么使用 newFixedThreadPool
线程数 <= 1.5 倍到 2 倍可用内核数。
来自 Java 文档:
Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.
Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.