Skip to content

Commit

Permalink
Fix findbugs errors in GlideExecutor.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Nov 2, 2015
1 parent 81ff043 commit 8a291a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
6 changes: 6 additions & 0 deletions library/findbugs-exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@
<Bug pattern="DM_DEFAULT_ENCODING" />
</Match>

<!-- We make a best effort attempt to acquire the cpu count from a fixed path -->
<Match>
<Class name="com.bumptech.glide.load.engine.executor.GlideExecutor" />
<Bug pattern="DMI_HARDCODED_ABSOLUTE_FILENAME" />
</Match>

</FindBugsFilter>
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,21 @@ private GlideExecutor(int corePoolSize, ThreadFactory threadFactory) {
* http://goo.gl/8H670N.
*/
public static int calculateBestThreadCount() {
File cpuInfo = new File(CPU_LOCATION);
final Pattern cpuNamePattern = Pattern.compile(CPU_NAME_REGEX);
File[] cpus = cpuInfo.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
return cpuNamePattern.matcher(s).matches();
File[] cpus = null;
try {
File cpuInfo = new File(CPU_LOCATION);
final Pattern cpuNamePattern = Pattern.compile(CPU_NAME_REGEX);
cpus = cpuInfo.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
return cpuNamePattern.matcher(s).matches();
}
});
} catch (Throwable t) {
if (Log.isLoggable(TAG, Log.ERROR)) {
Log.e(TAG, "Failed to calculate accurate cpu count", t);
}
});
}

int cpuCount = cpus != null ? cpus.length : 0;
int availableProcessors = Math.max(1, Runtime.getRuntime().availableProcessors());
Expand Down Expand Up @@ -164,7 +171,7 @@ protected void handle(Throwable t) {
private static final class DefaultThreadFactory implements ThreadFactory {
private final String name;
private final UncaughtThrowableStrategy uncaughtThrowableStrategy;
private int threadNum = 0;
private int threadNum;

DefaultThreadFactory() {
this(DEFAULT_NAME);
Expand All @@ -184,7 +191,7 @@ private static final class DefaultThreadFactory implements ThreadFactory {
}

@Override
public Thread newThread(@NonNull Runnable runnable) {
public synchronized Thread newThread(@NonNull Runnable runnable) {
final Thread result = new Thread(runnable, name + "-thread-" + threadNum) {
@Override
public void run() {
Expand All @@ -196,9 +203,7 @@ public void run() {
}
}
};
synchronized (this) {
threadNum++;
}
threadNum++;
return result;
}
}
Expand Down

0 comments on commit 8a291a8

Please sign in to comment.