LiteGo??????Android??????
LiteGo?????Java??????????????????????????????????????????????????????????????????????????????? LiteGo??????Runnable?Callable?FutureTask ??????????????????????SmartExecutor??????????App?????????????? ???App??SmartExecutor????????????????????????????????????????????????????????????????????????????? ??????????????????????????????????????????????????????????????
?? : litesuits.com
QQ? : 42960650
LiteGo ??
?????????????
- ??????????????????????????????????Java??????????????
- ???????App?????????????????????????Doug Lea??java???????????concurrent??
- ???????????????????????????????????????????????????????????????????????
???????????????????????????????
LiteGo ??
- ????????????????CPU??????????????????
- ??????????????CPU??????????????????
- ???????????????????????????????
???????????????????CPU????????CPU???????????????????????????CPU?????????CPU?????????
?????????????????????????????????????????
LiteGo ??
????????????????????????
????????????????????????????
?????????????????????????????
???????????????????
- ??????????
- ??????????
- ???????
- ????????????
- ????????????
LiteGo ??? OK?LET IT GO?
????
// ????????????[?????]??[????]?? SmartExecutor smallExecutor = new SmartExecutor();
// set temporary parameter just for test // ?????????????????????? // number of concurrent threads at the same time, recommended core size is CPU count // ???????????????????????????? smallExecutor.setCoreSize(2);
// adjust maximum number of waiting queue size by yourself or based on phone performance // ????????????????????????? smallExecutor.setQueueSize(2);
// ??????[?????]??????[????]???????????????????????????? smallExecutor.setSchedulePolicy(SchedulePolicy.LastInFirstRun);
// ???????????[????]????????????????????? smallExecutor.setOverloadPolicy(OverloadPolicy.DiscardOldTaskInQueue);
???????????????2??????????????????2?????????????????????????????????????????????
????????????
// ???? 4 ??? for (int i = 0; i < 4; i++) {
final int j = i;
smallExecutor.execute(new Runnable() {
@Override
public void run() {
HttpLog.i(TAG, " TASK " + j + " is running now ----------->");
SystemClock.sleep(j * 200);
}
}
);
}
// ???1?????????? Future future = smallExecutor.submit(new Runnable() {
@Override
public void run() {
HttpLog.i(TAG, " TASK 4 will be canceled... ------------>");
SystemClock.sleep(1000);
}
}
);
// ?????????? future.cancel(false);
??????????? 0?1?2?3?4 ???????4?????????????Future???
?????0?1???????????2?3???????????????????4???????????2????????3?4 ?
??4???????????????
TASK 0 is running now -----------> TASK 1 is running now -----------> TASK 3 is running now ----------->
LiteGO ????
??? SmartExecutor ????????
public Future<?> submit(Runnable task) public <T> Future<T> submit(Runnable task, T result) public <T> Future<T> submit(Callable<T> task) public <T> void submit(RunnableFuture<T> task) public void execute(final Runnable command)
????? execute ???????????????? FutureTask ??? execute ??????? FutureTask ?????? RunnableFuture ????? Runnable ? Future ???????
??????? execute ????
@Override public void execute(final Runnable command) {
if (command == null) {
return;
}
WrappedRunnable scheduler = new WrappedRunnable() {
@Override
public Runnable getRealRunnable() {
return command;
}
public Runnable realRunnable;
@Override
public void run() {
try {
command.run();
}
finally {
scheduleNext(this);
}
}
}
;
boolean callerRun = false;
synchronized (lock) {
if (runningList.size() < coreSize) {
runningList.add(scheduler);
threadPool.execute(scheduler);
}
else if (waitingList.size() < queueSize) {
waitingList.addLast(scheduler);
}
else {
switch (overloadPolicy) {
case DiscardNewTaskInQueue:
waitingList.pollLast();
waitingList.addLast(scheduler);
break;
case DiscardOldTaskInQueue:
waitingList.pollFirst();
waitingList.addLast(scheduler);
break;
case CallerRuns:
callerRun = true;
break;
case DiscardCurrentTask:
break;
case ThrowExecption:
throw new RuntimeException("Task rejected from lite smart executor. " + command.toString());
default:
break;
}
}
//printThreadPoolInfo();
}
if (callerRun) {
command.run();
}
}
??????????????
- ??????????“??”?????????????????
- ???????????????????????
- ????????????????????????????????
- ???????????????????????????
- ??????????????“??”???????????????????????
?????????????????????