From 16aa183b3f9d247548d3ff87ac01810763373de2 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 30 Jan 2020 17:05:15 +0100 Subject: [PATCH] Shutdown operations queue --- .../main/java/eu/faircode/email/Helper.java | 20 ++++++++++++++++++- .../eu/faircode/email/ServiceSynchronize.java | 8 +++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index 86e2a18768..669090c634 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -163,12 +163,14 @@ public class Helper { if (threads == 0) return new ThreadPoolExecutorEx( + name, 0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue(), factory); else if (threads == 1) return new ThreadPoolExecutorEx( + name, threads, threads, 0L, TimeUnit.MILLISECONDS, new PriorityBlockingQueue(10, new PriorityComparator()), @@ -184,6 +186,7 @@ public class Helper { }; else return new ThreadPoolExecutorEx( + name, threads, threads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue(), @@ -191,14 +194,29 @@ public class Helper { } private static class ThreadPoolExecutorEx extends ThreadPoolExecutor { - public ThreadPoolExecutorEx(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) { + String name; + + public ThreadPoolExecutorEx( + String name, + int corePoolSize, int maximumPoolSize, + long keepAliveTime, TimeUnit unit, + BlockingQueue workQueue, + ThreadFactory threadFactory) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); + this.name = name; } @Override protected void beforeExecute(Thread t, Runnable r) { Log.d("Executing " + t.getName()); } + + @Override + public void shutdown() { + Log.i("Shutdown " + name); + super.getQueue().clear(); + super.shutdown(); + } } private static class PriorityFuture implements RunnableFuture { diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index fb4c311ef4..1f74fb16cc 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -825,6 +825,9 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } }); + final ExecutorService executor = + Helper.getBackgroundExecutor(1, "account_" + account.id); + final Map mapFolders = new HashMap<>(); List idlers = new ArrayList<>(); try { @@ -944,9 +947,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences Core.onSynchronizeFolders(this, account, iservice.getStore(), state); // Open synchronizing folders - final ExecutorService executor = - Helper.getBackgroundExecutor(1, "account_" + account.id); - List folders = db.folder().getFolders(account.id, false, true); Collections.sort(folders, new Comparator() { @Override @@ -1416,6 +1416,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } }); + executor.shutdown(); + // Close folders for (EntityFolder folder : mapFolders.keySet()) if (folder.synchronize && !folder.poll && mapFolders.get(folder) != null) {