public class

IdlingThreadPoolExecutor

extends java.util.concurrent.ThreadPoolExecutor

implements IdlingResource

 java.lang.Object

↳java.util.concurrent.AbstractExecutorService

↳java.util.concurrent.ThreadPoolExecutor

↳androidx.test.espresso.idling.concurrent.IdlingThreadPoolExecutor

Gradle dependencies

compile group: 'androidx.test.espresso.idling', name: 'idling-concurrent', version: '3.6.1'

  • groupId: androidx.test.espresso.idling
  • artifactId: idling-concurrent
  • version: 3.6.1

Artifact androidx.test.espresso.idling:idling-concurrent:3.6.1 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test.espresso.idling:idling-concurrent com.android.support.test.espresso.idling:idling-concurrent

Androidx class mapping:

androidx.test.espresso.idling.concurrent.IdlingThreadPoolExecutor android.support.test.espresso.idling.concurrent.IdlingThreadPoolExecutor

Overview

A java.util.concurrent.ThreadPoolExecutor that can be registered as an IdlingResource with Espresso.

Overrides parent methods to monitor threads starting, and finishing execution. Uses CountingIdlingResource to track number of active tasks.

Summary

Constructors
publicIdlingThreadPoolExecutor(java.lang.String resourceName, int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, java.util.concurrent.ThreadFactory threadFactory)

Creates a new IdlingThreadPoolExecutor with the given initial parameters and default rejected execution handler.

Methods
protected voidafterExecute(java.lang.Runnable r, java.lang.Throwable t)

public synchronized voidexecute(java.lang.Runnable command)

public java.lang.StringgetName()

public booleanisIdleNow()

public voidregisterIdleTransitionCallback(IdlingResource.ResourceCallback callback)

protected voidterminated()

from java.util.concurrent.ThreadPoolExecutorallowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, shutdown, shutdownNow, toString
from java.util.concurrent.AbstractExecutorServiceinvokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
from java.lang.Objectclone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructors

public IdlingThreadPoolExecutor(java.lang.String resourceName, int corePoolSize, int maximumPoolSize, long keepAliveTime, java.util.concurrent.TimeUnit unit, java.util.concurrent.BlockingQueue<java.lang.Runnable> workQueue, java.util.concurrent.ThreadFactory threadFactory)

Creates a new IdlingThreadPoolExecutor with the given initial parameters and default rejected execution handler.

Parameters:

resourceName: the name of the executor (used for logging and idempotency of registration).
corePoolSize: the number of threads to keep in the pool, even if they are idle, unless allowCoreThreadTimeOut is set.
maximumPoolSize: the maximum number of threads to allow in the pool.
keepAliveTime: when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating.
unit: the time unit for the keepAliveTime argument.
workQueue: the queue to use for holding tasks before they are executed. This queue will hold only the Runnable tasks submitted by the execute method.
threadFactory: the factory to use when the executor creates a new thread.

Methods

public java.lang.String getName()

public boolean isIdleNow()

public void registerIdleTransitionCallback(IdlingResource.ResourceCallback callback)

public synchronized void execute(java.lang.Runnable command)

protected void afterExecute(java.lang.Runnable r, java.lang.Throwable t)

protected void terminated()

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.test.espresso.idling.concurrent;

import android.util.Log;
import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.IdlingResource;
import androidx.test.espresso.idling.CountingIdlingResource;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * A {@link ThreadPoolExecutor} that can be registered as an {@link IdlingResource} with Espresso.
 *
 * <p>Overrides parent methods to monitor threads starting, and finishing execution. Uses {@link
 * CountingIdlingResource} to track number of active tasks.
 */
public class IdlingThreadPoolExecutor extends ThreadPoolExecutor implements IdlingResource {

  private static final String LOG_TAG = "IdlingThreadPoolExec";

  private CountingIdlingResource countingIdlingResource;

  /**
   * Creates a new {@code IdlingThreadPoolExecutor} with the given initial parameters and default
   * rejected execution handler.
   *
   * @param resourceName the name of the executor (used for logging and idempotency of
   *     registration).
   * @param corePoolSize the number of threads to keep in the pool, even if they are idle, unless
   *     allowCoreThreadTimeOut is set.
   * @param maximumPoolSize the maximum number of threads to allow in the pool.
   * @param keepAliveTime when the number of threads is greater than the core, this is the maximum
   *     time that excess idle threads will wait for new tasks before terminating.
   * @param unit the time unit for the keepAliveTime argument.
   * @param workQueue the queue to use for holding tasks before they are executed. This queue will
   *     hold only the Runnable tasks submitted by the execute method.
   * @param threadFactory the factory to use when the executor creates a new thread.
   */
  public IdlingThreadPoolExecutor(
      String resourceName,
      int corePoolSize,
      int maximumPoolSize,
      long keepAliveTime,
      TimeUnit unit,
      BlockingQueue<Runnable> workQueue,
      ThreadFactory threadFactory) {
    super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    countingIdlingResource = new CountingIdlingResource(resourceName);
    Log.i(LOG_TAG, "Register idling resource for thread pool " + resourceName);
    IdlingRegistry.getInstance().register(this);
  }

  @Override
  public String getName() {
    return countingIdlingResource.getName();
  }

  @Override
  public boolean isIdleNow() {
    return countingIdlingResource.isIdleNow();
  }

  @Override
  public void registerIdleTransitionCallback(ResourceCallback callback) {
    countingIdlingResource.registerIdleTransitionCallback(callback);
  }

  @Override
  public synchronized void execute(Runnable command) {
    countingIdlingResource.increment();
    super.execute(command);
  }

  @Override
  protected void afterExecute(Runnable r, Throwable t) {
    countingIdlingResource.decrement();
  }

  @Override
  protected void terminated() {
    super.terminated();
    Log.i(LOG_TAG, "Thread pool terminated, unregistering " + countingIdlingResource.getName());
    IdlingRegistry.getInstance().unregister(this);
  }
}