public interface

TaskExecutor

 androidx.work.impl.utils.taskexecutor.TaskExecutor

Subclasses:

WorkManagerTaskExecutor

Gradle dependencies

compile group: 'androidx.work', name: 'work-runtime', version: '2.10.0-alpha03'

  • groupId: androidx.work
  • artifactId: work-runtime
  • version: 2.10.0-alpha03

Artifact androidx.work:work-runtime:2.10.0-alpha03 it located at Google repository (https://maven.google.com/)

Overview

Interface for executing common tasks in WorkManager.

Summary

Methods
public voidexecuteOnTaskThread(java.lang.Runnable runnable)

public java.util.concurrent.ExecutorgetMainThreadExecutor()

public SerialExecutorgetSerialTaskExecutor()

It wraps an executor passed in Configuration.getTaskExecutor() Executor must guarantee a serial execution.

public CoroutineDispatchergetTaskCoroutineDispatcher()

Methods

public java.util.concurrent.Executor getMainThreadExecutor()

Returns:

The java.util.concurrent.Executor for main thread task processing

public void executeOnTaskThread(java.lang.Runnable runnable)

Parameters:

runnable: java.lang.Runnable to execute on a thread pool used for internal book-keeping.

public SerialExecutor getSerialTaskExecutor()

It wraps an executor passed in Configuration.getTaskExecutor() Executor must guarantee a serial execution.

Returns:

The java.util.concurrent.Executor for internal book-keeping

public CoroutineDispatcher getTaskCoroutineDispatcher()

Source

/*
 * Copyright 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.work.impl.utils.taskexecutor;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.work.Configuration;

import java.util.concurrent.Executor;

import kotlinx.coroutines.CoroutineDispatcher;
import kotlinx.coroutines.ExecutorsKt;

/**
 * Interface for executing common tasks in WorkManager.
 *
 */

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public interface TaskExecutor {
    /**
     * @return The {@link Executor} for main thread task processing
     */
    @NonNull
    Executor getMainThreadExecutor();

    /**
     * @param runnable {@link Runnable} to execute on a thread pool used
     *                 for internal book-keeping.
     */
    default void executeOnTaskThread(@NonNull Runnable runnable) {
        getSerialTaskExecutor().execute(runnable);
    }

    /**
     * It wraps an executor passed in {@link Configuration#getTaskExecutor()}
     *
     * Executor must guarantee a serial execution.
     *
     * @return The {@link Executor} for internal book-keeping
     */
    @NonNull
    SerialExecutor getSerialTaskExecutor();

    @NonNull
    default CoroutineDispatcher getTaskCoroutineDispatcher() {
        return ExecutorsKt.from(getSerialTaskExecutor());
    }
}