目录:andorid jar/库源码解析

    用于链式执行跨线程代码,且传递数据

  1. Task.call(new Callable<Boolean>() {
  2. @Override
  3. public Boolean call() throws Exception {
  4. return true;
  5. }
  6. }, Task.UI_THREAD_EXECUTOR);
  7. Task.callInBackground(new Callable<Boolean>() {
  8. @Override
  9. public Boolean call() throws Exception {
  10. return false;
  11. }
  12. });
  13. Task.callInBackground(new Callable<Boolean>() {
  14. @Override
  15. public Boolean call() throws Exception {
  16. return true;
  17. }
  18. }).onSuccess(new Continuation<Boolean, Object>() {
  19. @Override
  20. public Object then(Task<Boolean> task) throws Exception {
  21. if (task.getResult()) {
  22. return null;
  23. } else {
  24. return new Object();
  25. }
  26. }
  27. }, Task.BACKGROUND_EXECUTOR).continueWith(new Continuation<Object, Object>() {
  28. @Override
  29. public Object then(Task<Object> task) throws Exception {
  30. if (task.getResult() == null) {
  31. Toast.makeText(getBaseContext(), "null", Toast.LENGTH_SHORT).show();
  32. } else {
  33. Toast.makeText(getBaseContext(), "not null", Toast.LENGTH_SHORT).show();
  34. }
  35. return null;
  36. }
  37. }, Task.UI_THREAD_EXECUTOR);

在内部通过维护多中 ExecutorService 对象,并且通过串联的方式进行调用。

并且通过维护内部变量在,在指定流程处,就是特定的,值,值通过Task的对象getResult拿到。

UIThread

  1. /**
  2. * An {@link java.util.concurrent.Executor} that runs tasks on the UI thread.
  3. */
  4. private static class UIThreadExecutor implements Executor {
  5. @Override
  6. public void execute(Runnable command) {
  7. new Handler(Looper.getMainLooper()).post(command);
  8. }
  9. }

BackgroundThread

  1. private BoltsExecutors() {
  2. background = !isAndroidRuntime()
  3. ? java.util.concurrent.Executors.newCachedThreadPool()
  4. : AndroidExecutors.newCachedThreadPool();
  5. scheduled = Executors.newSingleThreadScheduledExecutor();
  6. immediate = new ImmediateExecutor();
  7. }
  1. implementation 'com.parse.bolts:bolts-android:1.2.0'

版权声明:本文为Supperlitt原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Supperlitt/p/12833462.html