public abstract class

TaskExecutor

extends java.lang.Object

 java.lang.Object

↳androidx.arch.core.executor.TaskExecutor

Subclasses:

DefaultTaskExecutor, TaskExecutorWithFakeMainThread, ArchTaskExecutor

Gradle dependencies

compile group: 'androidx.arch.core', name: 'core-runtime', version: '2.1.0'

  • groupId: androidx.arch.core
  • artifactId: core-runtime
  • version: 2.1.0

Artifact androidx.arch.core:core-runtime:2.1.0 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.arch.core:core-runtime android.arch.core:runtime

Androidx class mapping:

androidx.arch.core.executor.TaskExecutor android.arch.core.executor.TaskExecutor

Overview

A task executor that can divide tasks into logical groups.

It holds a collection a executors for each group of task.

TODO: Don't use this from outside, we don't know what the API will look like yet.

Summary

Constructors
publicTaskExecutor()

Methods
public abstract voidexecuteOnDiskIO(java.lang.Runnable runnable)

Executes the given task in the disk IO thread pool.

public voidexecuteOnMainThread(java.lang.Runnable runnable)

Executes the given task on the main thread.

public abstract booleanisMainThread()

Returns true if the current thread is the main thread, false otherwise.

public abstract voidpostToMainThread(java.lang.Runnable runnable)

Posts the given task to the main thread.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public TaskExecutor()

Methods

public abstract void executeOnDiskIO(java.lang.Runnable runnable)

Executes the given task in the disk IO thread pool.

Parameters:

runnable: The runnable to run in the disk IO thread pool.

public abstract void postToMainThread(java.lang.Runnable runnable)

Posts the given task to the main thread.

Parameters:

runnable: The runnable to run on the main thread.

public void executeOnMainThread(java.lang.Runnable runnable)

Executes the given task on the main thread.

If the current thread is a main thread, immediately runs the given runnable.

Parameters:

runnable: The runnable to run on the main thread.

public abstract boolean isMainThread()

Returns true if the current thread is the main thread, false otherwise.

Returns:

true if we are on the main thread, false otherwise.

Source

/*
 * Copyright (C) 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.arch.core.executor;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;

/**
 * A task executor that can divide tasks into logical groups.
 * <p>
 * It holds a collection a executors for each group of task.
 * <p>
 * TODO: Don't use this from outside, we don't know what the API will look like yet.
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
public abstract class TaskExecutor {
    /**
     * Executes the given task in the disk IO thread pool.
     *
     * @param runnable The runnable to run in the disk IO thread pool.
     */
    public abstract void executeOnDiskIO(@NonNull Runnable runnable);

    /**
     * Posts the given task to the main thread.
     *
     * @param runnable The runnable to run on the main thread.
     */
    public abstract void postToMainThread(@NonNull Runnable runnable);

    /**
     * Executes the given task on the main thread.
     * <p>
     * If the current thread is a main thread, immediately runs the given runnable.
     *
     * @param runnable The runnable to run on the main thread.
     */
    public void executeOnMainThread(@NonNull Runnable runnable) {
        if (isMainThread()) {
            runnable.run();
        } else {
            postToMainThread(runnable);
        }
    }

    /**
     * Returns true if the current thread is the main thread, false otherwise.
     *
     * @return true if we are on the main thread, false otherwise.
     */
    public abstract boolean isMainThread();
}