| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.server; |
| 18 |
|
| 19 |
import android.os.Handler; |
| 20 |
import android.os.HandlerExecutor; |
| 21 |
import android.os.HandlerThread; |
| 22 |
import android.os.Looper; |
| 23 |
import android.os.Trace; |
| 24 |
|
| 25 |
import java.util.concurrent.Executor; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public final class JobSchedulerBackgroundThread extends HandlerThread { |
| 33 |
private static final long SLOW_DISPATCH_THRESHOLD_MS = 10_000; |
| 34 |
private static final long SLOW_DELIVERY_THRESHOLD_MS = 30_000; |
| 35 |
private static JobSchedulerBackgroundThread sInstance; |
| 36 |
private static Handler sHandler; |
| 37 |
private static Executor sHandlerExecutor; |
| 38 |
|
| 39 |
private JobSchedulerBackgroundThread() { |
| 40 |
super("jobscheduler.bg", android.os.Process.THREAD_PRIORITY_BACKGROUND); |
| 41 |
} |
| 42 |
|
| 43 |
private static void ensureThreadLocked() { |
| 44 |
if (sInstance == null) { |
| 45 |
sInstance = new JobSchedulerBackgroundThread(); |
| 46 |
sInstance.start(); |
| 47 |
final Looper looper = sInstance.getLooper(); |
| 48 |
looper.setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER); |
| 49 |
looper.setSlowLogThresholdMs( |
| 50 |
SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS); |
| 51 |
sHandler = new Handler(sInstance.getLooper()); |
| 52 |
sHandlerExecutor = new HandlerExecutor(sHandler); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
public static JobSchedulerBackgroundThread get() { |
| 58 |
synchronized (JobSchedulerBackgroundThread.class) { |
| 59 |
ensureThreadLocked(); |
| 60 |
return sInstance; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
public static Handler getHandler() { |
| 66 |
synchronized (JobSchedulerBackgroundThread.class) { |
| 67 |
ensureThreadLocked(); |
| 68 |
return sHandler; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
public static Executor getExecutor() { |
| 74 |
synchronized (JobSchedulerBackgroundThread.class) { |
| 75 |
ensureThreadLocked(); |
| 76 |
return sHandlerExecutor; |
| 77 |
} |
| 78 |
} |
| 79 |
} |