Skip to content

Commit

Permalink
feat(no-core-pool): add NoCorePool which without resident thread, you…
Browse files Browse the repository at this point in the history
… can customize the maximum number of threads that can run in the pool, and the amount of time that they wait while they are idle

Refs #1
  • Loading branch information
Jacksgong committed Oct 9, 2016
1 parent a478cd2 commit 7411a4d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package cn.dreamtobe.threadpool;

import android.annotation.TargetApi;
import android.os.Build;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
Expand All @@ -31,6 +34,20 @@ public class RealExecutors {
private static final IExecutor TEMPORARY_CACHED_THREAD_POOL = ThreadPools.
newCachedPool(5L, TimeUnit.SECONDS, "GlobalCachedThreadPool");

public static class NoCoreExecutor extends RealExecutor {

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public NoCoreExecutor(int threadCount, long keepAliveTime, TimeUnit unit,
String prefixName) {
super(threadCount, threadCount, keepAliveTime, unit,
new LinkedBlockingQueue<Runnable>(),
new AbortPolicy(),//never meet
prefixName);

allowCoreThreadTimeOut(true);
}
}

public static class ExceedWaitExecutor extends RealExecutor {
private final static String TAG = "ExceedWait";

Expand Down
33 changes: 31 additions & 2 deletions threadpool/src/main/java/cn/dreamtobe/threadpool/ThreadPools.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package cn.dreamtobe.threadpool;

import android.annotation.TargetApi;
import android.os.Build;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -55,8 +58,12 @@ public class ThreadPools {
public static IExecutor newExceedWaitPool(int corePoolSize,
int maximumPoolSize, long keepAliveTime, TimeUnit unit,
String prefixName) {
return new ThreadExecutor(new RealExecutors.
ExceedWaitExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, prefixName));
if (corePoolSize == 0) {
return newNoCorePool(maximumPoolSize, keepAliveTime, unit, prefixName);
}

return new ThreadExecutor(new RealExecutors.ExceedWaitExecutor(corePoolSize, maximumPoolSize,
keepAliveTime, unit, prefixName));
}

/**
Expand Down Expand Up @@ -163,4 +170,26 @@ public static IExecutor newCachedPool(long keepAliveTime, TimeUnit unit, String
return new ThreadExecutor(new RealExecutors.CachedPoolExecutor(keepAliveTime, unit, prefixName));
}

/**
* If there are {@code threadCount} tasks are running, the further task will be enqueued to the
* waiting queue, and will be executed when the size of running tasks less than {@code threadCount}.
* If the thread in this pool is turn to idle and the interval time of waiting for new tasks more
* than {@code keepAliveTime}, it will be terminate to reduce the cost of resources.
* <p>
* <strong>Note:</strong> This method is only available for GINGERBREAD or further version.
*
* @param threadCount the number of threads in pool.
* @param keepAliveTime the maximum time that excess idle threads will wait for new tasks before
* terminating.
* @param unit the time unit for the {@code keepAliveTime} argument.
* @param prefixName the prefix name of this thread pool.
* @return the executor of a new NoCore thread pool.
*/
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static IExecutor newNoCorePool(int threadCount, long keepAliveTime, TimeUnit unit,
String prefixName) {
return new ThreadExecutor(new RealExecutors.NoCoreExecutor(threadCount, keepAliveTime, unit,
prefixName));
}

}

0 comments on commit 7411a4d

Please sign in to comment.