Skip to content

Commit

Permalink
Avoid to create thread pool many times.
Browse files Browse the repository at this point in the history
  • Loading branch information
twolight committed Oct 14, 2015
1 parent 0f7810b commit 32f1f82
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions init/src/main/java/cn/zhaiyifan/init/Init.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Init {
private static Context sContext;
private static int mThreadPoolSize = DEFAULT_THREAD_POOL_SIZE;

private static ExecutorService mExecutorService;
/**
* Init with context.
*
Expand Down Expand Up @@ -87,7 +88,10 @@ public static Context getContext() {
* @param size thread pool size, value less or equal than 0 will produce a cached thread pool.
*/
public static void setThreadPoolSize(int size) {
mThreadPoolSize = size;
if(mThreadPoolSize != size){
mThreadPoolSize = size;
createThreadPool();
}
}

public static Flow getFlow(String flowName) {
Expand Down Expand Up @@ -157,10 +161,20 @@ public static int getFlowStatus(String flowName) {
* @return thread pool
*/
public static ExecutorService getThreadPool() {
if(mExecutorService == null){
createThreadPool();
}
return mExecutorService;
}

/**
* create a thread pool.
*/
public static void createThreadPool(){
if (mThreadPoolSize <= 0) {
return Executors.newCachedThreadPool();
mExecutorService = Executors.newCachedThreadPool();
} else {
return Executors.newFixedThreadPool(mThreadPoolSize);
mExecutorService = Executors.newFixedThreadPool(mThreadPoolSize);
}
}
}

0 comments on commit 32f1f82

Please sign in to comment.