本文介绍了Android的WorkManager的同时作业计数/最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WorkManager是否可以限制同时运行的作业数量?

Does WorkManager have a cap to the number of jobs that run at the same time?

非常简单的示例:

  • 单击按钮,创建10个一次性作业
  • 全部入队
  • 一次运行3个作业,而不是预期的全部10个
class MainActivity: AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)

        queue.setOnClickListener {
            val jobs = mutableListOf<OneTimeWorkRequest>()

            for( i in 1..10 ) {
                jobs += OneTimeWorkRequestBuilder<MyWorker>()
                    .setInputData(workDataOf("key" to i))
                    .build()
            }

            WorkManager.getInstance().enqueue(jobs)
        }
    }
}

class MyWorker: Worker() {
    override fun doWork(): Result {
        val jobId = inputData.getInt("key", -1)

        Log.d("worker", "starting job: $jobId")

        Completable.timer(10, SECONDS).blockingGet()

        Log.d("worker", "job finished: $jobId")

        return SUCCESS
    }
}

输出:

08-30 14:03:10.392  9825  9855 D worker  : starting job: 2
08-30 14:03:10.396  9825  9856 D worker  : starting job: 3
08-30 14:03:10.400  9825  9854 D worker  : starting job: 1
08-30 14:03:20.421  9825  9855 D worker  : job finished: 2
08-30 14:03:20.421  9825  9856 D worker  : job finished: 3
08-30 14:03:20.421  9825  9854 D worker  : job finished: 1
08-30 14:03:20.442  9825  9856 D worker  : starting job: 4
08-30 14:03:20.448  9825  9854 D worker  : starting job: 5
08-30 14:03:20.450  9825  9855 D worker  : starting job: 6
08-30 14:03:30.444  9825  9856 D worker  : job finished: 4
08-30 14:03:30.449  9825  9854 D worker  : job finished: 5
08-30 14:03:30.451  9825  9855 D worker  : job finished: 6
08-30 14:03:30.474  9825  9856 D worker  : starting job: 7
08-30 14:03:30.477  9825  9855 D worker  : starting job: 8
08-30 14:03:30.480  9825  9854 D worker  : starting job: 9
08-30 14:03:40.476  9825  9856 D worker  : job finished: 7
08-30 14:03:40.478  9825  9855 D worker  : job finished: 8
08-30 14:03:40.481  9825  9854 D worker  : job finished: 9
08-30 14:03:40.497  9825  9856 D worker  : starting job: 10
08-30 14:03:50.500  9825  9856 D worker  : job finished: 10

推荐答案

可以同时运行的作业数实际上由您配置的线程池确定.默认Executor此处.

The number of jobs that can run at the same time are actually determined by a thread pool that you configure. The default Executor is defined here.

通常,当您使用Worker基类时,会将Worker的实例与该Executor上的线程相关联.如果要更好地控制与Worker关联的线程,则可能需要查看CoroutineWorkerListenableWorker.

Typically when you are using the Worker base class, you are associating an instance of the Worker to a thread on this Executor. If you want greater control on which thread your Worker is associated with, you might want to take a look at CoroutineWorker or ListenableWorker.

默认Executor中的线程数由设备上的内核数确定.如果要同时运行所有10个作业,则必须执行以下操作:

The number of threads in the default Executor are determined by the number of cores on the device. If you want all 10 jobs to run at the same time you have to do the following:

  • 禁用默认的WorkManager初始化程序(通过为内容提供程序禁用清单合并).

  • Disable the default WorkManager initializer (by disabling manifest merging for the content provider).

Application.onCreate()或您自己的ContentProvider上初始化WorkManager.您需要在此处执行此操作,因为操作系统可以要求先前安排的Worker运行.有关更多信息,请参见这个.

Initialize WorkManager on Application.onCreate() or your own ContentProvider. You need to do this here because the OS can ask previously scheduled Workers to run. For more information look at this.

val configuration = Configuration.Builder()
    // Defines a thread pool with 10 threads.
    // Ideally you would choose a number that is dynamic based on the number
    // of cores on the device.
    .setExecutor(Executors.newFixedThreadPool(10))
    .build()

WorkManager.initialize(context, configuration)

在上面的示例中,我正在创建一个具有10个线程的固定大小的线程池(该线程池又可以处理10个Workers).现在,当您将Worker排入队列时,您将看到它们全部同时执行.

In the above example I am creating a fixed size thread pool with 10 threads (which in turn can handle 10 Workers). Now when you enqueue your Workers you will see all of them execute at the same time.

这篇关于Android的WorkManager的同时作业计数/最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:13