Skip to content

Commit

Permalink
use new thread pool
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenceDut committed Aug 14, 2018
1 parent 5a18a07 commit 3a34ff8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.silencedut.taskscheduler;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Process;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.SynchronousQueue;
Expand All @@ -36,6 +37,8 @@ public class TaskScheduler {
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
private static final long KEEP_ALIVE = 60L;
private static final BlockingQueue<Runnable> sPoolWorkQueue =
new LinkedBlockingQueue<Runnable>();

private static TaskScheduler getInstance() {
if(sTaskScheduler==null) {
Expand All @@ -53,7 +56,8 @@ private TaskScheduler() {
/*
mParallelExecutor 直接使用AsyncTask的线程,减少新线程创建带来的资源消耗
*/
mParallelExecutor = AsyncTask.THREAD_POOL_EXECUTOR;
mParallelExecutor = new ThreadPoolExecutor(CPU_COUNT,MAXIMUM_POOL_SIZE,
KEEP_ALIVE,TimeUnit.SECONDS,sPoolWorkQueue,ThreadFactory.TASKSCHEDULER_FACTORY);

/*
没有核心线程的线程池要用 SynchronousQueue 而不是LinkedBlockingQueue,SynchronousQueue是一个只有一个任务的队列,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
*/
class ThreadFactory {

static final java.util.concurrent.ThreadFactory TASKSCHEDULER_FACTORY = new java.util.concurrent.ThreadFactory() {
private final AtomicInteger mCount = new AtomicInteger(1);

@Override
public Thread newThread(Runnable r) {
Thread thread = new Thread(r, "TaskScheduler #" + mCount.getAndIncrement());
thread.setPriority(Process.THREAD_PRIORITY_BACKGROUND);
return thread;
}
};

static final java.util.concurrent.ThreadFactory TIME_OUT_THREAD_FACTORY = new java.util.concurrent.ThreadFactory() {
private final AtomicInteger mCount = new AtomicInteger(1);

Expand Down

0 comments on commit 3a34ff8

Please sign in to comment.