- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我见过大多数在 DataSource 类中使用 Executor 的示例,这些类通常从 ViewModel 传递到 DataSourceFactory,然后传递到 DataSource 类。我如何使用 executor。使用 Executor 有什么好处/优势。
View 模型
public FooViewModel{
public FooViewModel() {
executor = Executors.newFixedThreadPool(5);
//pasing executor here.
FooDataSourceFactory itemDataSourceFactory = new
FooDataSourceFactory(executor);
}
数据源工厂
public class FooDataSourceFactory extends DataSource.Factory {
private FooDataSource itemDataSource;
private Executor executor;
//creating the mutable live data
private MutableLiveData<FooDataSource> itemLiveDataSource = new
MutableLiveData<>();
public FooDataSourceFactory(Executor executor) {
this.executor = executor;
}
@Override
public DataSource create() {
//passing executor here..
itemDataSource = new FooDataSource(executor);
//posting the dataSource to get the values
itemLiveDataSource.postValue(itemDataSource);
//returning the dataSource
return itemDataSource;
.................
}
数据源
public class FooDataSource {
FooDataSource(Executor executor){
//don't know what to do with executor
}
}
最佳答案
An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An Executor is normally used instead of explicitly creating threads. For example, rather than invoking
new Thread(new RunnableTask()).start()
for each of a set of tasks, you might use:executor.execute(runnable);
在分页库中,目标是异步执行所有操作。
Executor 可以帮助您轻松插入和检索异步数据,而无需显式使用线程。
如果您正在开发具有 MVVM 架构的应用程序,您可能有一些“存储库”类来处理本地数据和/或 api 调用。在这种情况下,建议使用执行程序查询您的数据库。
您还可以使用执行程序来观察分页列表,以防您希望在数据更改时不在主线程上执行某些操作。
从本地数据库检索数据时,如 Room :
Out of the box room doesn't support database access on the main thread so the executor is there to ensure the work is done on a separate thread. reference
希望我的回答对您有所帮助。
关于android - android Paging Library中Executor的真正用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55401502/
Issue 2019/05/09 21:50:07.380 +0800 ERROR [ExecutorManager] [Azkaban] No active executors found
我的问题是:使用 Executors.newFixedThreadPool(1)?? 有意义吗? 。在两个线程(main + oneAnotherThread)场景下使用执行器服务是否高效?正在通过调
我想知道,Executors.newSingleThreadExecutor() 之间有什么区别?和 Executors.newFixedThreadPool(1) 以下摘自javadoc Unlik
我的问题是:使用 Executors.newFixedThreadPool(1) 有意义吗??。在两个线程(main + oneAnotherThread)场景中使用执行器服务是否有效?通过调用 ne
我有一个 Apache Spark 应用程序在集群模式下运行在 YARN 集群上(spark 在这个集群上有 3 个节点)。 当应用程序运行时,Spark-UI 显示 2 个执行程序(每个运行在不同的
我想知道是否有任何理由使用 Executor 而不是 ExecutorService。 据我所知,JDK 中没有实现 Executor 接口(interface),它也不是 ExecutorServi
我有多个使用 Celery Executor 的 dag,但我希望使用 Kubernetes Executor 运行一个特定的 dag。我无法推断出一种良好而可靠的方法来实现这一目标。 我有一个 ai
假设我们的 Controller 中有一个 Action 。在每次请求时,许多用户都会调用 performLogin。 def performLogin( ) = { Async {
创建和管理您自己的 ExecutorService 与使用 Spring Boot 的 @Async 方法和 @Bean 方法创建 Executor 添加一个@Bean来创建一个Executor 手动
问题从无到有,只有我在代码中所做的更改 - 安装了 RaSharper(但删除它并重新安装 Visual Studio 没有帮助)。 所以我使用 NUnit 3 来运行测试。 我有 Visual St
我们知道每个任务当时都在一个核心中执行。 假设我们有这样配置的节点集群: 10 节点。 每个节点 16 个核心。 每个节点 64 GB 内存。 我的问题是 有 1 个 16 核的执行程序和 16 个
我正在从 Jupyter Notebook 中初始化 PySpark,如下所示: from pyspark import SparkContext # conf = SparkConf().setAp
我正在向我的 Web 应用程序添加一个基于 Flask 的 API,以控制某些网络自动化功能的启动和停止。我遇到了一个奇怪的行为,即 Flask-Executor .submit() 方法调用的函数似
单元测试在本地运行良好。 在 Visual Studio 2017 托管生成代理上运行时,VSTest 任务失败并显示: 2018-12-08T10:42:16.3779907Z An excepti
我正在尝试制作一个执行器和线程的简单示例。 当我调用 newSingleThreadExecutor(new CustomThreadFactory) 时,一切顺利,但是当我使用 null 参数调用
对于一个线程,我通过以下代码段捕获未捕获的异常。但是,对于 ExecutorService executor = Executors.newFixedThreadPool(10);,如何捕获未捕获的异
我想创建一个 CompletableFuture,其返回值在 Kotlin 中的特定执行程序上运行。 下面的代码工作得很好。 return CompletableFuture.supplyAsync
考虑基本的固定线程池: Executors.newFixedThreadPool(MaxListeners) 我打算不断提交新任务 - 响应传入的 TCP 套接字服务请求。 然而,当每个任务中的Run
我们可以在定义 ThreadPoolExecutors 时提供 BlockingQueue 实现。但是,如果我使用工厂(执行器)创建单个线程池(如下所示),我想知道使用哪个阻塞队列。我猜它是一个 Li
我编写了一个程序来执行两个在 shell 前台运行的命令,直到在终端上按下 ^c。 外壳命令 ./weed master -mdir=/var/lib/qualebs/weed 上面命令的输出是 qu
我是一名优秀的程序员,十分优秀!