• andorid jar/库源码解析之Bolts


    目录:andorid jar/库源码解析 

    Bolts:

      作用:

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

      栗子:

    Task.call(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    return true;
                }
            }, Task.UI_THREAD_EXECUTOR);
    
            Task.callInBackground(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    return false;
                }
            });
    
            Task.callInBackground(new Callable<Boolean>() {
                @Override
                public Boolean call() throws Exception {
                    return true;
                }
            }).onSuccess(new Continuation<Boolean, Object>() {
                @Override
                public Object then(Task<Boolean> task) throws Exception {
                    if (task.getResult()) {
                        return null;
                    } else {
                        return new Object();
                    }
                }
            }, Task.BACKGROUND_EXECUTOR).continueWith(new Continuation<Object, Object>() {
                @Override
                public Object then(Task<Object> 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'
  • 相关阅读:
    Visual C# 2008+SQL Server 2005 数据库与网络开发14.1.2 WPF的组成
    Visual C# 2008+SQL Server 2005 数据库与网络开发13.1.3 简单记事本程序菜单设计
    Visual C# 2008+SQL Server 2005 数据库与网络开发13.1.1 菜单创建
    Feathers TextInput使KeyboardEvent失效
    UILabel 多行显示
    突破flash player的睡眠模式
    突破flash player睡眠模式 后续
    缩放UIImage
    IT菜鸟报到!
    用VMware装了Ubuntu后,安装VMware Tools
  • 原文地址:https://www.cnblogs.com/Supperlitt/p/12833462.html
Copyright © 2020-2023  润新知