Skip to content

Commit

Permalink
Merge pull request #2 from twolight/master
Browse files Browse the repository at this point in the history
Add finish callback method in task
  • Loading branch information
markzhai committed Oct 14, 2015
2 parents d5e5f9b + 32f1f82 commit f26011d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
4 changes: 4 additions & 0 deletions demo/src/main/java/cn/zhaiyifan/demo/DemoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ protected void start() {
public boolean runOnProcess(String processName) {
return processName.equals("cn.zhaiyifan.init");
}

@Override
protected void onResult() {
}
};

Task task2 = new Task("task2", false, 5) {
Expand Down
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);
}
}
}
41 changes: 40 additions & 1 deletion init/src/main/java/cn/zhaiyifan/init/Task.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package cn.zhaiyifan.init;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.util.concurrent.CountDownLatch;

/**
Expand All @@ -15,7 +18,8 @@ public abstract class Task implements Runnable {
private boolean mIsBlocked = true;
private long mDelay = 0;
private int mStatus = Status.STATUS_PENDING_START;

private static InternalHandler sHandler;
private static final int MESSAGE_POST_RUSULT = 0x1;
// for asynchronous task chain
private Task mParentTask;
private Task mChildTask;
Expand Down Expand Up @@ -92,6 +96,7 @@ public void run() {
long startTime = System.currentTimeMillis();

start();
getHandler().obtainMessage(MESSAGE_POST_RUSULT, this).sendToTarget();

long endTime = System.currentTimeMillis();
LogImpl.i(TAG, getName() + " runs " + (endTime - startTime));
Expand All @@ -107,6 +112,10 @@ public void run() {
mStatus = Status.STATUS_DONE;
}

/**
* This method is invoked by ui thread when the task finish.
*/
protected void onResult(){};
/**
* Run task.
*/
Expand Down Expand Up @@ -176,4 +185,34 @@ public boolean runOnProcess(String processName) {
public String getName() {
return mTaskName;
}

/**
* Provide a public handler for all the task;
* @return a handler object
*/
private static Handler getHandler() {
synchronized (Task.class) {
if (sHandler == null) {
sHandler = new InternalHandler();
}
return sHandler;
}
}

private static class InternalHandler extends Handler {
public InternalHandler() {
super(Looper.getMainLooper());
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_POST_RUSULT:
Task task = (Task) msg.obj;
task.onResult();
break;
default:
break;
}
}
}
}

0 comments on commit f26011d

Please sign in to comment.