Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bounded ThreadPoolExecutor for async notify calls #145

Merged
merged 3 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.android.tools.build:gradle:2.3.1'
}
}

Expand All @@ -26,7 +26,7 @@ android {
}

dependencies {
compile "com.android.support:support-annotations:25.1.1"
compile "com.android.support:support-annotations:25.3.1"
}

// Disable doclint:
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ POM_ARTIFACT_ID=bugsnag-android
POM_PACKAGING=aar

ANDROID_BUILD_TARGET_SDK_VERSION=21
ANDROID_BUILD_TOOLS_VERSION=23.0.1
ANDROID_BUILD_SDK_VERSION=23
ANDROID_BUILD_TOOLS_VERSION=25.0.2
ANDROID_BUILD_SDK_VERSION=25
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat Feb 11 05:32:33 CET 2017
#Thu Apr 20 12:01:45 PDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
30 changes: 27 additions & 3 deletions src/main/java/com/bugsnag/android/Async.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
package com.bugsnag.android;

import android.support.annotation.NonNull;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class Async {
// This is pretty much the same settings as AsyncTask#THREAD_POOL_EXECUTOR, except that it has
// a minimum of 1 for the core pool size, instead of 2. We could probably use
// AsyncTask.THREAD_POOL_EXECUTOR directly, but that requires API >= 11.
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int CORE_POOL_SIZE = Math.max(1, Math.min(CPU_COUNT - 1, 4));
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
private static final int KEEP_ALIVE_SECONDS = 30;
private static final BlockingQueue<Runnable> POOL_WORK_QUEUE =
new LinkedBlockingQueue<>(128);
private static final ThreadFactory THREAD_FACTORY = new ThreadFactory() {
private final AtomicInteger count = new AtomicInteger(1);

private static final Executor executor = Executors.newCachedThreadPool();
public Thread newThread(@NonNull Runnable r) {
return new Thread(r, "Bugsnag Thread #" + count.getAndIncrement());
}
};
private static final Executor EXECUTOR = new ThreadPoolExecutor(
CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
POOL_WORK_QUEUE, THREAD_FACTORY);

static void run(Runnable task) {
executor.execute(task);
EXECUTOR.execute(task);
}
}