From f6064ff19f12b08b8c2ddba83c96f73dfb5ff19f Mon Sep 17 00:00:00 2001 From: Fiouz Date: Sat, 2 Oct 2010 07:45:11 +0000 Subject: [PATCH] Use a thread pool to optimize resource usage instead of always firing up new threads (less garbage collection). Note: this is not a complete revert of r2153 since the pool is now automatically expanded as needed (different implementation). --- .../k9/controller/MessagingController.java | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/com/fsck/k9/controller/MessagingController.java b/src/com/fsck/k9/controller/MessagingController.java index dc447adc0..d8b03523f 100644 --- a/src/com/fsck/k9/controller/MessagingController.java +++ b/src/com/fsck/k9/controller/MessagingController.java @@ -18,6 +18,8 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.PriorityBlockingQueue; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; @@ -140,6 +142,8 @@ public class MessagingController implements Runnable ConcurrentHashMap pushers = new ConcurrentHashMap(); + private final ExecutorService threadPool = Executors.newCachedThreadPool(); + public enum SORT_TYPE { SORT_DATE(R.string.sort_earliest_first, R.string.sort_latest_first, false), @@ -401,7 +405,7 @@ public class MessagingController implements Runnable */ public void listFolders(final Account account, final boolean refreshRemote, final MessagingListener listener) { - new Thread(new Runnable() + threadPool.execute(new Runnable() { public void run() { @@ -457,7 +461,7 @@ public class MessagingController implements Runnable l.listFoldersFinished(account); } } - }).start(); + }); } private void doRefreshRemote(final Account account, MessagingListener listener) @@ -557,13 +561,13 @@ public class MessagingController implements Runnable */ public void listLocalMessages(final Account account, final String folder, final MessagingListener listener) { - new Thread(new Runnable() + threadPool.execute(new Runnable() { public void run() { listLocalMessagesSynchronous(account, folder, listener); } - }).start(); + }); } @@ -699,7 +703,7 @@ public class MessagingController implements Runnable + ")"); } - new Thread(new Runnable() + threadPool.execute(new Runnable() { public void run() { @@ -891,7 +895,7 @@ public class MessagingController implements Runnable listener.searchStats(stats); } } - }).start(); + }); } public void loadMoreMessages(Account account, String folder, MessagingListener listener) @@ -3101,7 +3105,7 @@ public class MessagingController implements Runnable { l.loadMessageForViewStarted(account, folder, uid); } - new Thread(new Runnable() + threadPool.execute(new Runnable() { public void run() { @@ -3159,7 +3163,7 @@ public class MessagingController implements Runnable } } - }).start(); + }); } /**