From 55e22e68b45657d97128f99f0f13bc8a16a9d61a Mon Sep 17 00:00:00 2001 From: Vincent Breitmoser Date: Thu, 29 Oct 2015 17:14:22 +0100 Subject: import: reduce number of threads a little, and code cleanup --- .../keychain/operations/ImportOperation.java | 76 +++++++++++----------- 1 file changed, 37 insertions(+), 39 deletions(-) (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations') diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportOperation.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportOperation.java index a04200ba1..948c0654e 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportOperation.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/operations/ImportOperation.java @@ -82,6 +82,8 @@ import org.sufficientlysecure.keychain.util.orbot.OrbotHelper; */ public class ImportOperation extends BaseOperation { + public static final int MAX_THREADS = 10; + public ImportOperation(Context context, ProviderHelper providerHelper, Progressable progressable) { super(context, providerHelper, progressable); @@ -412,61 +414,57 @@ public class ImportOperation extends BaseOperation { } @NonNull - private ImportKeyResult multiThreadedKeyImport(Iterator keyListIterator, + private ImportKeyResult multiThreadedKeyImport(@NonNull Iterator keyListIterator, int totKeys, final String keyServer, final Proxy proxy) { Log.d(Constants.TAG, "Multi-threaded key import starting"); - if (keyListIterator != null) { - KeyImportAccumulator accumulator = new KeyImportAccumulator(totKeys, mProgressable); + KeyImportAccumulator accumulator = new KeyImportAccumulator(totKeys, mProgressable); - final ProgressScaler ignoreProgressable = new ProgressScaler(); + final ProgressScaler ignoreProgressable = new ProgressScaler(); - final int maxThreads = 200; - ExecutorService importExecutor = new ThreadPoolExecutor(0, maxThreads, - 30L, TimeUnit.SECONDS, - new SynchronousQueue()); + ExecutorService importExecutor = new ThreadPoolExecutor(0, MAX_THREADS, 30L, TimeUnit.SECONDS, + new SynchronousQueue()); - ExecutorCompletionService importCompletionService = - new ExecutorCompletionService<>(importExecutor); + ExecutorCompletionService importCompletionService = + new ExecutorCompletionService<>(importExecutor); - while (keyListIterator.hasNext()) { // submit all key rings to be imported + while (keyListIterator.hasNext()) { // submit all key rings to be imported - final ParcelableKeyRing pkRing = keyListIterator.next(); + final ParcelableKeyRing pkRing = keyListIterator.next(); - Callable importOperationCallable = new Callable - () { + Callable importOperationCallable = new Callable + () { - @Override - public ImportKeyResult call() { + @Override + public ImportKeyResult call() { - ArrayList list = new ArrayList<>(); - list.add(pkRing); + ArrayList list = new ArrayList<>(); + list.add(pkRing); - return serialKeyRingImport(list.iterator(), 1, keyServer, ignoreProgressable, proxy); - } - }; + return serialKeyRingImport(list.iterator(), 1, keyServer, ignoreProgressable, proxy); + } + }; - importCompletionService.submit(importOperationCallable); - } + importCompletionService.submit(importOperationCallable); + } - while (!accumulator.isImportFinished()) { // accumulate the results of each import - try { - accumulator.accumulateKeyImport(importCompletionService.take().get()); - } catch (InterruptedException | ExecutionException e) { - Log.e(Constants.TAG, "A key could not be imported during multi-threaded " + - "import", e); - // do nothing? - if (e instanceof ExecutionException) { - // Since serialKeyRingImport does not throw any exceptions, this is what - // would have happened if - // we were importing the key on this thread - throw new RuntimeException(); - } + while (!accumulator.isImportFinished()) { // accumulate the results of each import + try { + accumulator.accumulateKeyImport(importCompletionService.take().get()); + } catch (InterruptedException | ExecutionException e) { + Log.e(Constants.TAG, "A key could not be imported during multi-threaded " + + "import", e); + // do nothing? + if (e instanceof ExecutionException) { + // Since serialKeyRingImport does not throw any exceptions, this is what + // would have happened if + // we were importing the key on this thread + throw new RuntimeException(); } } - return accumulator.getConsolidatedResult(); } - return new ImportKeyResult(ImportKeyResult.RESULT_FAIL_NOTHING, new OperationLog()); + return accumulator.getConsolidatedResult(); + } /** @@ -500,7 +498,7 @@ public class ImportOperation extends BaseOperation { } } - public synchronized void accumulateKeyImport(ImportKeyResult result) { + public void accumulateKeyImport(ImportKeyResult result) { mImportedKeys++; if (mProgressable != null) { -- cgit v1.2.3