66import android .support .annotation .NonNull ;
77import android .support .annotation .Nullable ;
88import android .util .Log ;
9+ import android .util .SparseArray ;
910
11+ import java .util .HashMap ;
1012import java .util .Map ;
11- import java .util .concurrent .ConcurrentHashMap ;
1213import java .util .concurrent .ExecutorService ;
1314import java .util .concurrent .Executors ;
1415import java .util .concurrent .LinkedBlockingQueue ;
2930 */
3031public final class ThreadUtils {
3132
32- private static final Map <Integer , Map <Integer , ExecutorService >> TYPE_PRIORITY_POOLS =
33- new ConcurrentHashMap <>();
34- private static final Map <Task , ScheduledExecutorService > TASK_SCHEDULED =
35- new ConcurrentHashMap <>();
33+ private static final SparseArray <SparseArray <ExecutorService >> TYPE_PRIORITY_POOLS = new SparseArray <>();
34+ private static final Map <Task , ScheduledExecutorService > TASK_SCHEDULED = new HashMap <>();
3635
3736 private static final byte TYPE_SINGLE = -1 ;
3837 private static final byte TYPE_CACHED = -2 ;
@@ -878,7 +877,7 @@ public void run() {
878877 }, initialDelay , period , unit );
879878 }
880879
881- private static ScheduledExecutorService getScheduledByTask (final Task task ) {
880+ private synchronized static ScheduledExecutorService getScheduledByTask (final Task task ) {
882881 ScheduledExecutorService scheduled = TASK_SCHEDULED .get (task );
883882 if (scheduled == null ) {
884883 UtilsThreadFactory factory = new UtilsThreadFactory ("scheduled" , Thread .MAX_PRIORITY );
@@ -888,7 +887,7 @@ private static ScheduledExecutorService getScheduledByTask(final Task task) {
888887 return scheduled ;
889888 }
890889
891- private static void removeScheduleByTask (final Task task ) {
890+ private synchronized static void removeScheduleByTask (final Task task ) {
892891 ScheduledExecutorService scheduled = TASK_SCHEDULED .get (task );
893892 if (scheduled != null ) {
894893 TASK_SCHEDULED .remove (task );
@@ -900,11 +899,11 @@ private static ExecutorService getPoolByTypeAndPriority(final int type) {
900899 return getPoolByTypeAndPriority (type , Thread .NORM_PRIORITY );
901900 }
902901
903- private static ExecutorService getPoolByTypeAndPriority (final int type , final int priority ) {
902+ private synchronized static ExecutorService getPoolByTypeAndPriority (final int type , final int priority ) {
904903 ExecutorService pool ;
905- Map < Integer , ExecutorService > priorityPools = TYPE_PRIORITY_POOLS .get (type );
904+ SparseArray < ExecutorService > priorityPools = TYPE_PRIORITY_POOLS .get (type );
906905 if (priorityPools == null ) {
907- priorityPools = new ConcurrentHashMap <>();
906+ priorityPools = new SparseArray <>();
908907 pool = createPoolByTypeAndPriority (type , priority );
909908 priorityPools .put (priority , pool );
910909 TYPE_PRIORITY_POOLS .put (type , priorityPools );
@@ -921,6 +920,7 @@ private static ExecutorService getPoolByTypeAndPriority(final int type, final in
921920 private static ExecutorService createPoolByTypeAndPriority (final int type , final int priority ) {
922921 switch (type ) {
923922 case TYPE_SINGLE :
923+ System .out .println ("hehe" );
924924 return Executors .newSingleThreadExecutor (
925925 new UtilsThreadFactory ("single" , priority )
926926 );
0 commit comments