Skip to content

Commit 30a7ba8

Browse files
authored
Merge b662152 into 3cf020a
2 parents 3cf020a + b662152 commit 30a7ba8

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

firebase-common/src/main/java/com/google/firebase/concurrent/CustomThreadFactory.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,38 @@
1414

1515
package com.google.firebase.concurrent;
1616

17+
import android.os.Process;
18+
import android.os.StrictMode;
1719
import java.util.Locale;
1820
import java.util.concurrent.Executors;
1921
import java.util.concurrent.ThreadFactory;
2022
import java.util.concurrent.atomic.AtomicLong;
23+
import javax.annotation.Nullable;
2124

2225
class CustomThreadFactory implements ThreadFactory {
2326
private static final ThreadFactory DEFAULT = Executors.defaultThreadFactory();
2427
private final AtomicLong threadCount = new AtomicLong();
2528
private final String namePrefix;
2629
private final int priority;
30+
private final StrictMode.ThreadPolicy policy;
2731

28-
CustomThreadFactory(String namePrefix, int priority) {
32+
CustomThreadFactory(String namePrefix, int priority, @Nullable StrictMode.ThreadPolicy policy) {
2933
this.namePrefix = namePrefix;
3034
this.priority = priority;
35+
this.policy = policy;
3136
}
3237

3338
@Override
3439
public Thread newThread(Runnable r) {
35-
Thread thread = DEFAULT.newThread(r);
36-
thread.setPriority(priority);
40+
Thread thread =
41+
DEFAULT.newThread(
42+
() -> {
43+
Process.setThreadPriority(priority);
44+
if (policy != null) {
45+
StrictMode.setThreadPolicy(policy);
46+
}
47+
r.run();
48+
});
3749
thread.setName(
3850
String.format(Locale.ROOT, "%s Thread #%d", namePrefix, threadCount.getAndIncrement()));
3951
return thread;

firebase-common/src/main/java/com/google/firebase/concurrent/ExecutorsRegistrar.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
package com.google.firebase.concurrent;
1616

1717
import android.annotation.SuppressLint;
18+
import android.os.Build;
1819
import android.os.Process;
20+
import android.os.StrictMode;
21+
import com.google.firebase.BuildConfig;
1922
import com.google.firebase.annotations.concurrent.Background;
2023
import com.google.firebase.annotations.concurrent.Blocking;
2124
import com.google.firebase.annotations.concurrent.Lightweight;
@@ -40,15 +43,17 @@ public class ExecutorsRegistrar implements ComponentRegistrar {
4043
() ->
4144
scheduled(
4245
Executors.newFixedThreadPool(
43-
4, factory("Firebase Background", Process.THREAD_PRIORITY_BACKGROUND))));
46+
4,
47+
factory(
48+
"Firebase Background", Process.THREAD_PRIORITY_BACKGROUND, bgPolicy()))));
4449

4550
private static final Lazy<ScheduledExecutorService> LITE_EXECUTOR =
4651
new Lazy<>(
4752
() ->
4853
scheduled(
4954
Executors.newFixedThreadPool(
5055
Math.max(2, Runtime.getRuntime().availableProcessors()),
51-
factory("Firebase Lite", Process.THREAD_PRIORITY_DEFAULT))));
56+
factory("Firebase Lite", Process.THREAD_PRIORITY_DEFAULT, litePolicy()))));
5257

5358
private static final Lazy<ScheduledExecutorService> BLOCKING_EXECUTOR =
5459
new Lazy<>(
@@ -97,6 +102,33 @@ private static ScheduledExecutorService scheduled(ExecutorService delegate) {
97102
}
98103

99104
private static ThreadFactory factory(String threadPrefix, int priority) {
100-
return new CustomThreadFactory(threadPrefix, priority);
105+
return new CustomThreadFactory(threadPrefix, priority, null);
106+
}
107+
108+
private static ThreadFactory factory(
109+
String threadPrefix, int priority, StrictMode.ThreadPolicy policy) {
110+
return new CustomThreadFactory(threadPrefix, priority, policy);
111+
}
112+
113+
private static StrictMode.ThreadPolicy bgPolicy() {
114+
StrictMode.ThreadPolicy.Builder builder = new StrictMode.ThreadPolicy.Builder().detectNetwork();
115+
if (Build.VERSION.SDK_INT >= 23) {
116+
builder.detectResourceMismatches();
117+
if (Build.VERSION.SDK_INT >= 26) {
118+
builder.detectUnbufferedIo();
119+
}
120+
}
121+
if (BuildConfig.DEBUG) {
122+
builder.penaltyDeath();
123+
}
124+
return builder.penaltyLog().build();
125+
}
126+
127+
private static StrictMode.ThreadPolicy litePolicy() {
128+
StrictMode.ThreadPolicy.Builder builder = new StrictMode.ThreadPolicy.Builder().detectAll();
129+
if (BuildConfig.DEBUG) {
130+
builder.penaltyDeath();
131+
}
132+
return builder.penaltyLog().build();
101133
}
102134
}

0 commit comments

Comments
 (0)