Skip to content

Commit

Permalink
Add status track
Browse files Browse the repository at this point in the history
  • Loading branch information
markzhai committed Oct 4, 2015
1 parent 5d55dd9 commit 05e9031
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ multi-process, tidy magic code for every process, and improves efficiency of app
It is originally designed for application initialization, but not confined to that, it can be
applied to any complex initialization procedure.

([For Chinese 中文戳这里](https://github.com/markzhai/init/blob/master/README_CN.md))
[For Chinese 中文戳这里](https://github.com/markzhai/init/blob/master/README_CN.md)

# Usage

Expand Down
14 changes: 10 additions & 4 deletions init/src/main/java/cn/zhaiyifan/appinit/Flow.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Flow {
private static final long DEFAULT_FLOW_TIMEOUT = 3000;

private SparseArray<Wave> mWaveArray;
private Map<String, Integer> mTaskStatusMap;
private Map<String, Integer> mTaskToWaveMap;

private int mFlowStatus = Status.STATUS_UNKNOWN;

Expand All @@ -37,7 +37,7 @@ public Flow(String flowName) {
mFlowStatus = Status.STATUS_PENDING_START;

mWaveArray = new SparseArray<>();
mTaskStatusMap = new HashMap<>();
mTaskToWaveMap = new HashMap<>();
}

/**
Expand All @@ -55,6 +55,7 @@ public Flow addTask(int waveSeq, Task task) {
mWaveArray.put(waveSeq, wave);
}
wave.addTask(task);
mTaskToWaveMap.put(task.getName(), waveSeq);
}
return this;
}
Expand Down Expand Up @@ -122,8 +123,13 @@ public int getFlowStatus() {
* @return status
*/
public int getTaskStatus(String taskName) {
Integer status = mTaskStatusMap.get(taskName);
return status != null ? status : Status.STATUS_UNKNOWN;
Integer waveSeq = mTaskToWaveMap.get(taskName);
Wave wave = mWaveArray.get(waveSeq);
if (wave != null) {
return wave.getTaskStatus(taskName);
} else {
return Status.STATUS_UNKNOWN;
}
}

/**
Expand Down
6 changes: 1 addition & 5 deletions init/src/main/java/cn/zhaiyifan/appinit/ProcessUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.List;

class ProcessUtils {
public static boolean isValidProcessName = false;
private static volatile String sProcessName;
private final static Object sNameLock = new Object();

Expand Down Expand Up @@ -38,9 +37,6 @@ public static boolean isMainProcess() {
return false;
}
Context context = Init.getContext();
if (processName.length() < 5 || context.getApplicationInfo().processName.length() < 5) {
isValidProcessName = true;
}
sMainProcess = processName.equals(context.getApplicationInfo().processName);
return sMainProcess;
}
Expand All @@ -59,4 +55,4 @@ private static String obtainProcessName(Context context) {
}
return null;
}
}
}
39 changes: 39 additions & 0 deletions init/src/main/java/cn/zhaiyifan/appinit/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,55 @@ public abstract class Task implements Runnable {
private CountDownLatch mDoneSignal;
private boolean mIsBlocked = true;
private long mDelay = 0;
private int mStatus = Status.STATUS_PENDING_START;

/**
* Constructor
*
* @param name task name
*/
public Task(String name) {
mTaskName = name;
}

/**
* Constructor
*
* @param name task name
* @param delay task delay
*/
public Task(String name, long delay) {
mTaskName = name;
mDelay = delay;
}

/**
* Constructor
*
* @param name task name
* @param isBlocked if task is blocked
*/
public Task(String name, boolean isBlocked) {
mTaskName = name;
mIsBlocked = isBlocked;
}

/**
* Constructor
*
* @param name task name
* @param isBlocked if task is blocked
* @param delay task delay
*/
public Task(String name, boolean isBlocked, long delay) {
mTaskName = name;
mIsBlocked = isBlocked;
mDelay = delay;
}

/**
* Normally should not override it
*/
@Override
public void run() {
if (mDelay > 0) {
Expand All @@ -44,6 +72,7 @@ public void run() {
LogImpl.w(TAG, getName() + ": " + e.getMessage());
}
}
mStatus = Status.STATUS_EXECUTING;

long startTime = System.currentTimeMillis();

Expand All @@ -55,6 +84,7 @@ public void run() {
if (mDoneSignal != null) {
mDoneSignal.countDown();
}
mStatus = Status.STATUS_DONE;
}

/**
Expand Down Expand Up @@ -85,6 +115,15 @@ public long getDelay() {
return mDelay;
}

/**
* Get task status
*
* @return status
*/
public int getStatus() {
return mStatus;
}

/**
* Determine task's process, by default returns true, which means run on all processes.
*
Expand Down
43 changes: 32 additions & 11 deletions init/src/main/java/cn/zhaiyifan/appinit/Wave.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,21 @@
* <h1>A wave is made up of several tasks.</h1>
* Created by mark.zhai on 2015/10/2.
*/
class Wave {

public class Wave {
private static final String TAG = "Wave";
private static final long DEFAULT_WAVE_TIMEOUT = 1500;


private long mTimeout = DEFAULT_WAVE_TIMEOUT;

private List<Task> mTaskList = new LinkedList<>();

private String mProcessName;

private int mSequence;
private long mTimeout = DEFAULT_WAVE_TIMEOUT;
private int mStatus = Status.STATUS_UNKNOWN;

public Wave(int sequence, String processName) {
this.mSequence = sequence;
this.mProcessName = processName;
mSequence = sequence;
mProcessName = processName;
mStatus = Status.STATUS_PENDING_START;
}

/**
Expand All @@ -38,6 +36,30 @@ public Wave addTask(Task task) {
return this;
}

/**
* Get wave status.
*
* @return status
*/
public int getStatus() {
return mStatus;
}

/**
* Get task status.
*
* @param taskName task name
* @return status
*/
public int getTaskStatus(String taskName) {
for (Task task : mTaskList) {
if (task.getName().equals(taskName)) {
return task.getStatus();
}
}
return Status.STATUS_UNKNOWN;
}

/**
* Set timeout to this wave.
*
Expand All @@ -49,10 +71,8 @@ public Wave setTimeout(long timeout) {
}

public void start() {

mStatus = Status.STATUS_EXECUTING;
List<Task> blockTaskList = new LinkedList<>();


ExecutorService threadPool = Executors.newCachedThreadPool();

for (Task task : mTaskList) {
Expand Down Expand Up @@ -80,5 +100,6 @@ public void start() {
LogImpl.w(TAG, "Wave " + mSequence + "await interrupted. " + e.getMessage());
}
}
mStatus = Status.STATUS_DONE;
}
}

0 comments on commit 05e9031

Please sign in to comment.