public enum TaskState {
init, checkedRequest, checkedResource, resourceLack, checkedCopyFinish, unZip, runCalcul, checkedNewOutput, zipAndCopyOutput, checkedProcessState, end, error,
}
自定义的enum类,
使用一般使用enum都是switch吧?
反正我是这么用的
public void run() {
TaskState ta = TaskState.init;
while (shutdownRequested) {
switch (ta) {
//init state
case init:
if (checkRequest() != 0) {
ta = TaskState.checkedRequest;
sleepTime(config.getWAITTIMEN1());
}
else{
sleepTime(config.getWAITTIMEN1());
}
break;
//a new Request file find
case checkedRequest:
if (checkConditions() == 0) {
ta = TaskState.resourceLack;
} else {
ta = TaskState.checkedResource;
}
break;
//compute resource lack
case resourceLack:
outputResourceLack();
clearFolder();
ta = TaskState.init;
break;
//compute resource OK
case checkedResource:
if (checkedCopyFinish() == 0) {
} else {
ta = TaskState.checkedCopyFinish;
}
break;
//compute file zip copy finished
case checkedCopyFinish:
unzipFolder();
ta = TaskState.unZip;
break;
//unzip ok
case unZip:
runCalcul();
ta = TaskState.runCalcul;
break;
//run calcul.exe
case runCalcul:
int i = checkProcessState();
if (i == 1) {
ta = TaskState.checkedProcessState;
}
if (i == 2) {
ta = TaskState.end;
}
if (i == 2) {
ta = TaskState.error;
}
break;
//the calcul process is running
case checkedProcessState:
if (checkNewOutput() == 0) {
ta = TaskState.runCalcul;
} else {
ta = TaskState.checkedNewOutput;
}
break;
//a new intermediate results output
case checkedNewOutput:
zipFolder();
ta = TaskState.runCalcul;
break;
//the process end
case end:
zipFolder();
clearFolder();
ta = TaskState.init;
break;
//the process error
case error:
zipFolder();
clearFolder();
ta = TaskState.init;
break;
default:
//write answer file ,and thread sleep
updateAnswer();
break;
}
}
}
大家随便看看吧,比较简单的switch配合 enum