Andorid jar 라 이브 러 리 소스 볼트 원리 분석

Bolts:
역할:
크로스 스 레 드 코드 를 체인 으로 실행 하고 데 이 터 를 전달 합 니 다.
밤:
Task.call(new Callable() {
@Override
public Boolean call() throws Exception {
return true;
}
}, Task.UI_THREAD_EXECUTOR);
Task.callInBackground(new Callable() {
@Override
public Boolean call() throws Exception {
return false;
}
});
Task.callInBackground(new Callable() {
@Override
public Boolean call() throws Exception {
return true;
}
}).onSuccess(new Continuation() {
@Override
public Object then(Task task) throws Exception {
if (task.getResult()) {
return null;
} else {
return new Object();
}
}
}, Task.BACKGROUND_EXECUTOR).continueWith(new Continuation() {
@Override
public Object then(Task task) throws Exception {
if (task.getResult() == null) {
Toast.makeText(getBaseContext(), "null", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getBaseContext(), "not null", Toast.LENGTH_SHORT).show();
}
return null;
}
}, Task.UI_THREAD_EXECUTOR);
원본 코드 판독:
내부 에서 다 중 ExecutorService 대상 을 유지 하고 직렬 로 호출 합 니 다.
또한 내부 변 수 를 유지 함으로써 지정 한 절차 에서 특정한'값'을 Task 의 대상 getResult 를 통 해 가 져 옵 니 다.
  UIThread
/**
* An {@link java.util.concurrent.Executor} that runs tasks on the UI thread.
*/
private static class UIThreadExecutor implements Executor {
@Override
public void execute(Runnable command) {
new Handler(Looper.getMainLooper()).post(command);
}
}
  BackgroundThread
private BoltsExecutors() {
background = !isAndroidRuntime()
? java.util.concurrent.Executors.newCachedThreadPool()
: AndroidExecutors.newCachedThreadPool();
scheduled = Executors.newSingleThreadScheduledExecutor();
immediate = new ImmediateExecutor();
}
원본 코드:https://github.com/BoltsFramework/Bolts-Android
도입:
implementation'com.parse.bolts:bolts-android:1.2.0'이상 이 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주시 기 바 랍 니 다.

좋은 웹페이지 즐겨찾기