public class

ArchTaskExecutor

extends TaskExecutor

 java.lang.Object

androidx.arch.core.executor.TaskExecutor

↳androidx.arch.core.executor.ArchTaskExecutor

Gradle dependencies

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

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

Artifact androidx.arch.core:core-runtime:2.2.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.ArchTaskExecutor android.arch.core.executor.ArchTaskExecutor

Overview

A static class that serves as a central point to execute common tasks.

Summary

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

Executes the given task in the disk IO thread pool.

public static ArchTaskExecutorgetInstance()

Returns an instance of the task executor.

public static java.util.concurrent.ExecutorgetIOThreadExecutor()

public static java.util.concurrent.ExecutorgetMainThreadExecutor()

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.

public voidsetDelegate(TaskExecutor taskExecutor)

Sets a delegate to handle task execution requests.

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

Methods

public static ArchTaskExecutor getInstance()

Returns an instance of the task executor.

Returns:

The singleton ArchTaskExecutor.

public void setDelegate(TaskExecutor taskExecutor)

Sets a delegate to handle task execution requests.

If you have a common executor, you can set it as the delegate and App Toolkit components will use your executors. You may also want to use this for your tests.

Calling this method with null sets it to the default TaskExecutor.

Parameters:

taskExecutor: The task executor to handle task requests.

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 static java.util.concurrent.Executor getMainThreadExecutor()

public static java.util.concurrent.Executor getIOThreadExecutor()

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.Nullable;
import androidx.annotation.RestrictTo;

import java.util.concurrent.Executor;

/**
 * A static class that serves as a central point to execute common tasks.
 * <p>
 *
 * @hide This API is not final.
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX)
public class ArchTaskExecutor extends TaskExecutor {
    private static volatile ArchTaskExecutor sInstance;

    @NonNull
    private TaskExecutor mDelegate;

    @NonNull
    private final TaskExecutor mDefaultTaskExecutor;

    @NonNull
    private static final Executor sMainThreadExecutor =
            command -> getInstance().postToMainThread(command);

    @NonNull
    private static final Executor sIOThreadExecutor =
            command -> getInstance().executeOnDiskIO(command);

    private ArchTaskExecutor() {
        mDefaultTaskExecutor = new DefaultTaskExecutor();
        mDelegate = mDefaultTaskExecutor;
    }

    /**
     * Returns an instance of the task executor.
     *
     * @return The singleton ArchTaskExecutor.
     */
    @NonNull
    public static ArchTaskExecutor getInstance() {
        if (sInstance != null) {
            return sInstance;
        }
        synchronized (ArchTaskExecutor.class) {
            if (sInstance == null) {
                sInstance = new ArchTaskExecutor();
            }
        }
        return sInstance;
    }

    /**
     * Sets a delegate to handle task execution requests.
     * <p>
     * If you have a common executor, you can set it as the delegate and App Toolkit components will
     * use your executors. You may also want to use this for your tests.
     * <p>
     * Calling this method with {@code null} sets it to the default TaskExecutor.
     *
     * @param taskExecutor The task executor to handle task requests.
     */
    public void setDelegate(@Nullable TaskExecutor taskExecutor) {
        mDelegate = taskExecutor == null ? mDefaultTaskExecutor : taskExecutor;
    }

    @Override
    public void executeOnDiskIO(@NonNull Runnable runnable) {
        mDelegate.executeOnDiskIO(runnable);
    }

    @Override
    public void postToMainThread(@NonNull Runnable runnable) {
        mDelegate.postToMainThread(runnable);
    }

    @NonNull
    public static Executor getMainThreadExecutor() {
        return sMainThreadExecutor;
    }

    @NonNull
    public static Executor getIOThreadExecutor() {
        return sIOThreadExecutor;
    }

    @Override
    public boolean isMainThread() {
        return mDelegate.isMainThread();
    }
}