RunUtil

public class RunUtil
extends Object implements IRunUtil

java.lang.Object
   ↳ com.android.tradefed.util.RunUtil


用于执行操作的一组辅助方法。

摘要

常量

String INHERITIO_PREFIX

String RUNNABLE_NOTIFIER_NAME

公共构造函数

RunUtil()

创建一个新的 RunUtil 对象以供使用。

RunUtil(boolean inheritEnvVars)

公共方法

void allowInterrupt(boolean allow)

允许/禁止在当前线程上运行中断。

ProcessBuilder createProcessBuilder(Redirect redirect, commandList, boolean enableCache)
static IRunUtil getDefault()

获取对默认 RunUtil 对象的引用。

void interrupt(Thread thread, String message, ErrorIdentifier errorId)

中断给定线程上正在进行/即将进行的运行操作。

void interrupt(Thread thread, String message)

中断给定线程上正在进行/即将进行的运行操作。

boolean isInterruptAllowed()

提供 RunUtil 的中断状态。

Process runCmdInBackground(Redirect redirect, command)

一种替代的 runCmdInBackground(String) 方法,可接受 ERROR(/List) 形式的命令实参。

Process runCmdInBackground( command)

一种替代的 runCmdInBackground(String) 方法,可接受 ERROR(/List) 形式的命令实参。

Process runCmdInBackground( command, OutputStream output)

运行命令并使用 ERROR(/OutputStream) 记录命令的输出。

Process runCmdInBackground(String... command)

用于异步执行系统命令的辅助方法。

Process runCmdInBackground(Redirect redirect, String... command)

用于异步执行系统命令的辅助方法。

boolean runEscalatingTimedRetry(long opTimeout, long initialPollInterval, long maxPollInterval, long maxTime, IRunUtil.IRunnableResult runnable)

阻塞并多次执行操作,直到成功为止。

boolean runFixedTimedRetry(long opTimeout, long pollInterval, long maxTime, IRunUtil.IRunnableResult runnable)

阻塞并多次执行操作,直到成功为止。

boolean runFixedTimedRetryWithOutputMonitor(long opTimeout, long idleOutputTimeout, long pollInterval, long maxTime, IRunUtil.IRunnableResult runnable)

阻塞并多次执行操作,直到成功为止。

CommandStatus runTimed(long timeout, IRunUtil.IRunnableResult runnable, boolean logErrors)

阻塞并执行操作,如果操作时间超过指定时间,则中止操作。

CommandResult runTimedCmd(long timeout, OutputStream stdout, OutputStream stderr, String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行,并根据需要将输出重定向到文件。

CommandResult runTimedCmd(long timeout, String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

CommandResult runTimedCmdRetry(long timeout, long retryInterval, int attempts, String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

CommandResult runTimedCmdRetryWithOutputMonitor(long timeout, long idleOutputTimeout, long retryInterval, int attempts, String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

CommandResult runTimedCmdSilently(long timeout, String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

CommandResult runTimedCmdSilentlyRetry(long timeout, long retryInterval, int attempts, String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

CommandResult runTimedCmdWithInput(long timeout, String input, File stdoutFile, File stderrFile, String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行,并根据需要将输出重定向到文件。

CommandResult runTimedCmdWithInput(long timeout, String input, String... command)

用于执行需要标准输入 (stdin) 的系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

CommandResult runTimedCmdWithInput(long timeout, String input, command)

用于执行需要标准输入 (stdin) 的系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

CommandResult runTimedCmdWithInputRedirect(long timeout, File inputRedirect, String... command)

用于执行需要从文件重定向 Stdin 的系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

CommandResult runTimedCmdWithOutputMonitor(long timeout, long idleOutputTimeout, OutputStream stdout, OutputStream stderr, String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行,并根据需要将输出重定向到文件。

CommandResult runTimedCmdWithOutputMonitor(long timeout, long idleOutputTimeout, String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

boolean runTimedRetry(long opTimeout, long pollInterval, int attempts, IRunUtil.IRunnableResult runnable)

阻塞并多次执行操作,直到成功为止。

boolean runTimedRetryWithOutputMonitor(long opTimeout, long idleOutputTimeout, long pollInterval, int attempts, IRunUtil.IRunnableResult runnable)

阻塞并多次执行操作,直到成功为止。

CommandStatus runTimedWithOutputMonitor(long timeout, long idleOutputTimeout, IRunUtil.IRunnableResult runnable, boolean logErrors)

阻塞并执行操作,如果操作时间超过指定时间,则中止操作。

void setEnvVariable(String name, String value)

设置在运行系统命令时使用的环境变量。

void setEnvVariablePriority(IRunUtil.EnvPriority priority)

决定在创建进程时,取消设置环境变量的优先级是否高于设置环境变量。

void setInterruptibleInFuture(Thread thread, long timeMs)

在等待一段时间后设置为可中断。

void setLinuxInterruptProcess(boolean interrupt)

允许在通过 #runTimed 方法运行的进程达到超时时,使用 Linux“kill”中断。

void setRedirectStderrToStdout(boolean redirect)

在运行系统命令时,将标准错误流设置为重定向到标准输出流。

void setWorkingDir(File dir)

为系统命令设置工作目录。

void sleep(long time)

用于休眠指定时间的辅助方法,忽略任何异常。

void unsetEnvVariable(String key)

取消设置环境变量,以便系统命令在没有此环境变量的情况下运行。 环境变量可能会从父进程继承,因此我们需要从 ProcessBuilder.environment() 中删除环境变量

常量

INHERITIO_PREFIX

public static final String INHERITIO_PREFIX

常量值: "inheritio-"

RUNNABLE_NOTIFIER_NAME

public static final String RUNNABLE_NOTIFIER_NAME

常量值: “RunnableNotifier”

公共构造函数

RunUtil

public RunUtil ()

创建要使用的新 RunUtil 对象。

RunUtil

public RunUtil (boolean inheritEnvVars)

参数
inheritEnvVars boolean

公共方法

allowInterrupt

public void allowInterrupt (boolean allow)

允许/禁止在当前线程上运行中断。如果允许,则当前线程的运行操作可被其他线程通过 interrupt(Thread, String) 方法中断。

参数
allow boolean:是否允许在当前线程上中断运行。

createProcessBuilder

public ProcessBuilder createProcessBuilder (Redirect redirect, 
                 commandList, 
                boolean enableCache)

参数
redirect Redirect

commandList

enableCache boolean

返回
ProcessBuilder

getDefault

public static IRunUtil getDefault ()

获取对默认 RunUtil 对象的引用。

这对于想要使用 IRunUtil 但不想进行自定义的调用方很有用。建议需要自定义 IRunUtil 实例(即需要调用 setEnvVariable(String, String)setWorkingDir(File))的调用者创建自己的副本。

返回
IRunUtil

中断

public void interrupt (Thread thread, 
                String message, 
                ErrorIdentifier errorId)

中断给定线程上正在进行/即将进行的运行操作。相应线程上的运行操作将抛出 RunInterruptedException

参数
message String:针对 RunInterruptedException 的消息。

errorId ErrorIdentifier:表示中断的原因(如果已知)。

中断

public void interrupt (Thread thread, 
                String message)

中断给定线程上正在进行/即将进行的运行操作。相应线程上的运行操作将抛出 RunInterruptedException

参数
message String:针对 RunInterruptedException 的消息。

isInterruptAllowed

public boolean isInterruptAllowed ()

提供 RunUtil 的中断状态。

返回
boolean 如果 Run 可以中断,则为 true,否则为 false。

runCmdInBackground

public Process runCmdInBackground (Redirect redirect, 
                 command)

一种替代的 runCmdInBackground(String) 方法,可接受 ERROR(/List) 形式的命令实参。

参数
redirect Redirect:要应用于 ProcessBuilderERROR(/Redirect)

command :包含指定系统命令和可选参数以供执行的 ERROR(/List)

返回
Process 已执行命令的 Process

runCmdInBackground

public Process runCmdInBackground ( command)

一种替代的 runCmdInBackground(String) 方法,可接受 ERROR(/List) 形式的命令实参。

参数
command :包含指定系统命令和可选参数以供执行的 ERROR(/List)

返回
Process 已执行命令的 Process

runCmdInBackground

public Process runCmdInBackground ( command, 
                OutputStream output)

运行带有 ERROR(/OutputStream) 的命令,记录命令的输出。 stdout 和 stderr 合并在一起。

参数
command :要运行的命令

output OutputStream:用于保存输出的 OutputStream

返回
Process 运行命令的 Process

runCmdInBackground

public Process runCmdInBackground (String... command)

用于异步执行系统命令的辅助方法。

会在启动命令后立即返回。

参数
command String:要执行的指定系统命令和可选实参

返回
Process 已执行命令的 Process

runCmdInBackground

public Process runCmdInBackground (Redirect redirect, 
                String... command)

用于异步执行系统命令的辅助方法。

会在启动命令后立即返回。

参数
redirect Redirect:要应用于 ProcessBuilderERROR(/Redirect)

command String:要执行的指定系统命令和可选实参

返回
Process 已执行命令的 Process

runEscalatingTimedRetry

public boolean runEscalatingTimedRetry (long opTimeout, 
                long initialPollInterval, 
                long maxPollInterval, 
                long maxTime, 
                IRunUtil.IRunnableResult runnable)

阻塞并多次执行操作,直到成功为止。

以指数方式增加操作尝试之间的等待时间。此参数旨在用于执行轮询服务器等操作时,以便在服务器暂时停机时为其提供恢复时间。

参数
opTimeout long:单次操作尝试的最长等待时间(以毫秒为单位)

initialPollInterval long:操作尝试之间的初始等待时间

maxPollInterval long:操作尝试之间的最长等待时间

maxTime long:尝试执行操作的总近似最长时间

runnable IRunUtil.IRunnableResultIRunUtil.IRunnableResult 执行

返回
boolean 如果操作在 maxTime 过期之前成功完成,则返回 true

runFixedTimedRetry

public boolean runFixedTimedRetry (long opTimeout, 
                long pollInterval, 
                long maxTime, 
                IRunUtil.IRunnableResult runnable)

阻塞并多次执行操作,直到成功为止。

参数
opTimeout long:单次操作尝试的最长等待时间(以毫秒为单位)

pollInterval long:操作尝试之间的初始等待时间

maxTime long:尝试执行操作的总大致最长时间

runnable IRunUtil.IRunnableResultIRunUtil.IRunnableResult 执行

返回
boolean 如果操作在 maxTime 过期之前成功完成,则返回 true

runFixedTimedRetryWithOutputMonitor

public boolean runFixedTimedRetryWithOutputMonitor (long opTimeout, 
                long idleOutputTimeout, 
                long pollInterval, 
                long maxTime, 
                IRunUtil.IRunnableResult runnable)

阻塞并多次执行操作,直到成功为止。还会监控输出流的活动,如果在指定时间内未观察到任何流活动,则中止。如果 idleOutputTimeout 设置为零,则不会进行任何流监控。

参数
opTimeout long:单次操作尝试的最长等待时间(以毫秒为单位)

idleOutputTimeout long:在输出流上等待输出的最长时间(以毫秒为单位)

pollInterval long:操作尝试之间的初始等待时间

maxTime long:尝试执行操作的总大致最长时间

runnable IRunUtil.IRunnableResultIRunUtil.IRunnableResult 执行

返回
boolean 如果操作在 maxTime 过期之前成功完成,则返回 true

runTimed

public CommandStatus runTimed (long timeout, 
                IRunUtil.IRunnableResult runnable, 
                boolean logErrors)

阻塞并执行操作,如果操作时间超过指定时间,则中止。

参数
timeout long:等待时间上限(以毫秒为单位)

runnable IRunUtil.IRunnableResultIRunUtil.IRunnableResult 执行

logErrors boolean:是否记录异常错误。

返回
CommandStatus 操作的 CommandStatus 结果。

runTimedCmd

public CommandResult runTimedCmd (long timeout, 
                OutputStream stdout, 
                OutputStream stderr, 
                String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行,并根据指定将输出重定向到文件。如果以这种方式提供 ERROR(/OutputStream),则在函数结束时,它们将保持打开状态。

参数
timeout long:超时最长等待时间(以毫秒为单位)。0 表示不超时。

stdout OutputStream:标准输出将重定向到的 ERROR(/OutputStream)。可以为 null。

stderr OutputStream:错误输出将重定向到的 ERROR(/OutputStream)。可以为 null。

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmd

public CommandResult runTimedCmd (long timeout, 
                String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止。

参数
timeout long:等待的最长时间(以毫秒为单位)。0 表示不超时。

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmdRetry

public CommandResult runTimedCmdRetry (long timeout, 
                long retryInterval, 
                int attempts, 
                String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止。

参数
timeout long:每次尝试等待的最长时间(以毫秒为单位)

retryInterval long:命令重试之间的等待时间

attempts int:尝试次数上限

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmdRetryWithOutputMonitor

public CommandResult runTimedCmdRetryWithOutputMonitor (long timeout, 
                long idleOutputTimeout, 
                long retryInterval, 
                int attempts, 
                String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止。还会监控输出流的活动,如果在指定时间内未观察到任何流活动,则中止。如果 idleOutputTimeout 设置为零,则不会进行任何流监控。

参数
timeout long:每次尝试等待的最长时间(以毫秒为单位)

idleOutputTimeout long:在输出流上等待输出的最长时间(以毫秒为单位)

retryInterval long:命令重试之间的等待时间

attempts int:尝试次数上限

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmdSilently

public CommandResult runTimedCmdSilently (long timeout, 
                String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止。与 runTimedCmd(long, String) 类似,但不会在出现异常时记录任何错误。

参数
timeout long:等待时间上限(以毫秒为单位)

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmdSilentlyRetry

public CommandResult runTimedCmdSilentlyRetry (long timeout, 
                long retryInterval, 
                int attempts, 
                String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止。与 runTimedCmdRetry(long, long, int, String[]) 类似,但不会在出现异常时记录任何错误。

参数
timeout long:等待时间上限(以毫秒为单位)

retryInterval long:命令重试之间的等待时间

attempts int:尝试次数上限

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmdWithInput

public CommandResult runTimedCmdWithInput (long timeout, 
                String input, 
                File stdoutFile, 
                File stderrFile, 
                String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行,并根据指定将输出重定向到文件。

参数
timeout long:超时最长等待时间(以毫秒为单位)。0 表示不超时。

input String:要传递给进程的 stdin 输入

stdoutFile File:标准输出将重定向到的 ERROR(/File)。可以为 null。

stderrFile File:错误输出将重定向到的 ERROR(/File)。可以为 null。

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmdWithInput

public CommandResult runTimedCmdWithInput (long timeout, 
                String input, 
                String... command)

用于执行需要标准输入 (stdin) 的系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

参数
timeout long:等待时间上限(以毫秒为单位)

input String:要传递给进程的 stdin 输入

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmdWithInput

public CommandResult runTimedCmdWithInput (long timeout, 
                String input, 
                 command)

用于执行需要标准输入 (stdin) 的系统命令的辅助方法,如果执行时间超过指定时间,则中止执行。

参数
timeout long:等待时间上限(以毫秒为单位)

input String:要传递给进程的 stdin 输入

command :包含系统命令和可选的要执行的实参的 ERROR(/List)

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmdWithInputRedirect

public CommandResult runTimedCmdWithInputRedirect (long timeout, 
                File inputRedirect, 
                String... command)

用于执行需要从文件重定向 Stdin 的系统命令的辅助方法,如果该命令的执行时间超过指定时间,则中止执行。

参数
timeout long:等待时间上限(以毫秒为单位)

inputRedirect File:要使用 ProcessBuilder.redirectInput() 重定向为标准输入的 ERROR(/File)。如果为 null,则不会重定向标准输入。

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmdWithOutputMonitor

public CommandResult runTimedCmdWithOutputMonitor (long timeout, 
                long idleOutputTimeout, 
                OutputStream stdout, 
                OutputStream stderr, 
                String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止执行,并根据指定将输出重定向到文件。如果以这种方式提供 ERROR(/OutputStream),它们将在函数结束时保持打开状态。

参数
timeout long:超时最长等待时间(以毫秒为单位)。0 表示不超时。

idleOutputTimeout long:在输出流上等待输出的最长时间(以毫秒为单位)

stdout OutputStream:标准输出将重定向到的 ERROR(/OutputStream)。可以为 null。

stderr OutputStream:错误输出将重定向到的 ERROR(/OutputStream)。可以为 null。

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedCmdWithOutputMonitor

public CommandResult runTimedCmdWithOutputMonitor (long timeout, 
                long idleOutputTimeout, 
                String... command)

用于执行系统命令的辅助方法,如果执行时间超过指定时间,则中止。还会监控输出流的活动,如果在指定时间内未观察到任何流活动,则中止。如果 idleOutputTimeout 设置为零,则不会进行任何流监控。

参数
timeout long:等待的最长时间(以毫秒为单位)。0 表示不超时。

idleOutputTimeout long:在输出流上等待输出的最长时间(以毫秒为单位)

command String:要执行的指定系统命令和可选实参

返回
CommandResult 包含命令运行结果的 CommandResult

runTimedRetry

public boolean runTimedRetry (long opTimeout, 
                long pollInterval, 
                int attempts, 
                IRunUtil.IRunnableResult runnable)

阻塞并多次执行操作,直到成功为止。

参数
opTimeout long:一次操作尝试的最长等待时间(以毫秒为单位)

pollInterval long:命令重试之间的等待时间

attempts int:尝试次数上限

runnable IRunUtil.IRunnableResultIRunUtil.IRunnableResult 执行

返回
boolean 如果操作在达到尝试次数上限之前成功完成,则为 true

runTimedRetryWithOutputMonitor

public boolean runTimedRetryWithOutputMonitor (long opTimeout, 
                long idleOutputTimeout, 
                long pollInterval, 
                int attempts, 
                IRunUtil.IRunnableResult runnable)

阻塞并多次执行操作,直到成功为止。还会监控输出流的活动,如果在指定时间内未观察到任何流活动,则中止。如果 idleOutputTimeout 设置为零,则不会进行任何流监控。

参数
opTimeout long:一次操作尝试的最长等待时间(以毫秒为单位)

idleOutputTimeout long:在输出流上等待输出的最长时间(以毫秒为单位)

pollInterval long:命令重试之间的等待时间

attempts int:尝试次数上限

runnable IRunUtil.IRunnableResultIRunUtil.IRunnableResult 执行

返回
boolean 如果操作在达到尝试次数上限之前成功完成,则为 true

runTimedWithOutputMonitor

public CommandStatus runTimedWithOutputMonitor (long timeout, 
                long idleOutputTimeout, 
                IRunUtil.IRunnableResult runnable, 
                boolean logErrors)

阻塞并执行操作,如果操作时间超过指定时间,则中止。还会监控输出流的活动,如果在指定时间内未观察到任何流活动,则中止。如果 idleOutputTimeout 设置为零,则不会进行任何流监控。

参数
timeout long:等待时间上限(以毫秒为单位)

idleOutputTimeout long:在输出流上等待输出的最长时间(以毫秒为单位)

runnable IRunUtil.IRunnableResultIRunUtil.IRunnableResult 执行

logErrors boolean:是否记录异常错误。

返回
CommandStatus 操作的 CommandStatus 结果。

setEnvVariable

public void setEnvVariable (String name, 
                String value)

设置在运行系统命令时使用的环境变量。

参数
name String:变量名称

value String:变量值

setEnvVariablePriority

public void setEnvVariablePriority (IRunUtil.EnvPriority priority)

决定在创建进程时,取消设置环境变量的优先级是否高于设置环境变量。默认情况下,取消设置的优先级更高:这意味着,如果尝试设置同名变量,则不会成功,因为该变量将被取消设置。无法在默认 IRunUtil 实例上使用。

参数
priority IRunUtil.EnvPriority

setInterruptibleInFuture

public void setInterruptibleInFuture (Thread thread, 
                long timeMs)

在等待一段时间后设置为可中断。 ERROR(/CommandScheduler#shutdownHard()) 以确保我们最终会终止。

参数
thread Thread:将变为可中断的线程。

timeMs long:在设置为可中断之前等待的时间。

setLinuxInterruptProcess

public void setLinuxInterruptProcess (boolean interrupt)

允许在通过 #runTimed 方法运行的进程达到超时时,使用 Linux“kill”中断。 无法在默认 IRunUtil 实例上使用。

参数
interrupt boolean

setRedirectStderrToStdout

public void setRedirectStderrToStdout (boolean redirect)

设置在运行系统命令时将标准错误流重定向到标准输出流。初始值为 false。

参数
redirect boolean:是否重定向的新值

setWorkingDir

public void setWorkingDir (File dir)

设置系统命令的工作目录。

参数
dir File:工作目录

sleep

public void sleep (long time)

用于休眠指定时间(忽略任何异常)的辅助方法。

参数
time long:休眠时间(以毫秒为单位)。系统会忽略小于或等于 0 的值

unsetEnvVariable

public void unsetEnvVariable (String key)

取消设置环境变量,以便系统命令在没有此环境变量的情况下运行。 环境变量可能会从父进程继承,因此我们需要从 ProcessBuilder.environment() 中删除环境变量

参数
key String:变量名称