IRunUtil

public interface IRunUtil

com.android.tradefed.util.IRunUtil


用于运行定时操作和系统命令的接口。

摘要

嵌套类

interface IRunUtil.IRunnableResult

用于异步执行返回布尔值状态的操作的接口。 

公共方法

abstract void allowInterrupt(boolean allow)

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

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

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

abstract void interrupt(Thread thread, String message)

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

abstract boolean isInterruptAllowed()

提供 RunUtil 的中断状态。

abstract Process runCmdInBackground(Redirect redirect, command)

另一个 runCmdInBackground(String) 方法,可接受 ERROR(/List) 形式的命令参数。

abstract Process runCmdInBackground( command)

另一个 runCmdInBackground(String) 方法,可接受 ERROR(/List) 形式的命令参数。

abstract Process runCmdInBackground( command, OutputStream output)

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

abstract Process runCmdInBackground(String... command)

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

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

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

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

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

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

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

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

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

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

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

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

一种辅助方法,用于执行系统命令;如果所用时间超过指定时间,则中止,并将输出重定向到文件(如果已指定)。

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

用于执行系统命令,并在花费的时间超过指定时间时中止的辅助方法。

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

用于执行系统命令,并在花费的时间超过指定时间时中止的辅助方法。

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

用于执行系统命令,并在花费的时间超过指定时间时中止的辅助方法。

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

用于执行系统命令,并在花费的时间超过指定时间时中止的辅助方法。

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

用于执行系统命令,并在花费的时间超过指定时间时中止的辅助方法。

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

一种辅助方法,用于执行系统命令;如果所用时间超过指定时间,则中止,并将输出重定向到文件(如果已指定)。

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

一种辅助方法,用于执行需要 stdin 输入的系统命令,并在执行时间超过指定时间时中止系统命令。

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

一种辅助方法,用于执行需要 stdin 输入的系统命令,并在执行时间超过指定时间时中止系统命令。

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

用于执行系统命令的辅助方法,该命令需要从文件中重定向标准输入文件,并在执行时间超过指定时间时中止它。

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

一种辅助方法,用于执行系统命令;如果所用时间超过指定时间,则中止,并将输出重定向到文件(如果已指定)。

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

用于执行系统命令,并在花费的时间超过指定时间时中止的辅助方法。

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

使用缓存执行系统命令的辅助方法。

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

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

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

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

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

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

abstract void setEnvVariable(String key, String value)

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

abstract void setEnvVariablePriority(IRunUtil.EnvPriority priority)

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

abstract void setInterruptibleInFuture(Thread thread, long timeMs)

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

abstract void setLinuxInterruptProcess(boolean interrupt)

允许在 Linux 通过 #runTimed 方法运行的进程超时时“终止”中断。

abstract void setRedirectStderrToStdout(boolean redirect)

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

abstract void setWorkingDir(File dir)

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

abstract void sleep(long time)

帮助程序方法在给定时间内休眠,忽略所有异常。

abstract void unsetEnvVariable(String key)

取消设置环境变量,以便系统命令在没有此环境变量的情况下运行。

公共方法

allowInterrupt(允许中断)

public abstract void allowInterrupt (boolean allow)

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

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

中断

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

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

参数
message StringRunInterruptedException 的消息。

errorId ErrorIdentifier:表示已知中断原因。

中断

public abstract void interrupt (Thread thread, 
                String message)

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

参数
message StringRunInterruptedException 的消息。

isInterruptAllowed

public abstract boolean isInterruptAllowed ()

提供 RunUtil 的中断状态。

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

运行 CmdInBackground

public abstract Process runCmdInBackground (Redirect redirect, 
                 command)

另一个 runCmdInBackground(String) 方法,可接受 ERROR(/List) 形式的命令参数。

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

command :包含指定系统命令以及 exec 的参数(可选)的 ERROR(/List)

返回
Process 已执行命令的 Process

抛出
if 命令未能运行

运行 CmdInBackground

public abstract Process runCmdInBackground ( command)

另一个 runCmdInBackground(String) 方法,可接受 ERROR(/List) 形式的命令参数。

参数
command :包含指定系统命令以及 exec 的参数(可选)的 ERROR(/List)

返回
Process 已执行命令的 Process

抛出
if 命令未能运行

运行 CmdInBackground

public abstract Process runCmdInBackground ( command, 
                OutputStream output)

使用 ERROR(/OutputStream) 运行命令会记录命令的输出。stdout 和 stderr 会合并在一起。

参数
command :要运行的命令

output OutputStream:用于保存输出的 OutputStream

返回
Process 运行命令的 Process

抛出
IOException

运行 CmdInBackground

public abstract Process runCmdInBackground (String... command)

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

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

参数
command String:指定的系统命令和 exec 的可选参数

返回
Process 已执行命令的 Process

抛出
if 命令未能运行

运行 CmdInBackground

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

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

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

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

command String:指定的系统命令和 exec 的可选参数

返回
Process 已执行命令的 Process

抛出
if 命令未能运行

runEscalatingTimedRetry 返回

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

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

以指数方式增加操作尝试之间的等待时间。这应在执行轮询服务器等操作时使用,以便在服务器暂时关闭时为其留出恢复时间。

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

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

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

maxTime long:继续尝试操作的大致总时长上限

runnable IRunUtil.IRunnableResult:要执行的 IRunUtil.IRunnableResult

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

runFixedTimed 重试

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

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

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

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

maxTime long:继续尝试操作的大致总时长上限

runnable IRunUtil.IRunnableResult:要执行的 IRunUtil.IRunnableResult

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

runFixedTimedRetryWithOutputMonitor

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

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

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

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

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

maxTime long:继续尝试操作的大致总时长上限

runnable IRunUtil.IRunnableResult:要执行的 IRunUtil.IRunnableResult

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

runTimed 键

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

阻塞并执行操作,如果所需时间超过指定时间,则取消。

参数
timeout long:最长等待时间(毫秒)

runnable IRunUtil.IRunnableResult:要执行的 IRunUtil.IRunnableResult

logErrors boolean:记录异常与非异常相关的错误。

返回
CommandStatus 运算的 CommandStatus 结果。

runTimedCmd

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

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

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

stdout OutputStreamERROR(/OutputStream),标准输出将被重定向到该位置。可以为 null。

stderr OutputStreamERROR(/OutputStream),错误输出将被重定向到该位置。可以为 null。

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmd

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

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

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

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmd 重试

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

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

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

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

attempts int:尝试次数上限

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmd 使用输出监视器重试

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

用于执行系统命令的辅助方法,如果所用时间超过指定时间,则中止。它还会监控输出流中的活动,如果在指定时间内未观察到任何流活动,则中止。如果空闲输出超时值设为零,则不会发生数据流监控。

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

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

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

attempts int:尝试次数上限

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmdSilently(静默运行)

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

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

参数
timeout long:最长等待时间(毫秒)

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmd 静默地重试

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

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

参数
timeout long:最长等待时间(毫秒)

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

attempts int:尝试次数上限

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmdWithInput(在输入时运行)

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

一种辅助方法,用于执行系统命令;如果所用时间超过指定时间,则中止,并将输出重定向到文件(如果已指定)。

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

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

stdoutFile FileERROR(/File),标准输出将被重定向到该位置。可以为 null。

stderrFile FileERROR(/File),错误输出将被重定向到该位置。可以为 null。

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmdWithInput(在输入时运行)

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

一种辅助方法,用于执行需要 stdin 输入的系统命令,并在执行时间超过指定时间时中止系统命令。

参数
timeout long:最长等待时间(毫秒)

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

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmdWithInput(在输入时运行)

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

一种辅助方法,用于执行需要 stdin 输入的系统命令,并在执行时间超过指定时间时中止系统命令。

参数
timeout long:最长等待时间(毫秒)

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

command :包含系统命令和 exec(可选)参数的 ERROR(/List)

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

runTimedCmdWithInputRedirect 类

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

用于执行系统命令的辅助方法,该命令要求从文件重定向标准输入,并在耗时超过指定时间时中止。

参数
timeout long:最长等待时间(毫秒)

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

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmdWithOutputMonitor

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

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

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

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

stdout OutputStreamERROR(/OutputStream),标准输出将被重定向到该位置。可以为 null。

stderr OutputStreamERROR(/OutputStream),错误输出将被重定向到该位置。可以为 null。

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmdWithOutputMonitor

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

用于执行系统命令的辅助方法,如果所用时间超过指定时间,则中止。它还会监控输出流中的活动,如果在指定时间内未观察到任何流活动,则中止。如果空闲输出超时值设为零,则不会发生数据流监控。

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

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

command String:指定的系统命令和 exec 的可选参数

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

runTimedCmdWithOutputMonitor

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

使用缓存执行系统命令的辅助方法。

如果指定 cacheClient,则将启用缓存。如果缓存可用,将返回缓存的结果。否则,系统将使用 runTimedCmdWithOutputMonitor(long, long, OutputStream, OutputStream, String) 来执行该命令,并上传结果以进行缓存。

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

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

stdout OutputStreamERROR(/OutputStream),标准输出将被重定向到该位置。可以为 null。

stderr OutputStreamERROR(/OutputStream),错误输出将被重定向到该位置。可以为 null。

cacheClient ICacheClient:用于处理缓存的 ICacheClient 实例。

command String:指定的系统命令和 exec 的可选参数。

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

runTimedRetry [运行时间重试]

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

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

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

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

attempts int:尝试次数上限

runnable IRunUtil.IRunnableResult:要执行的 IRunUtil.IRunnableResult

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

runTimedRetryWithOutputMonitor

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

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

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

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

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

attempts int:尝试次数上限

runnable IRunUtil.IRunnableResult:要执行的 IRunUtil.IRunnableResult

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

runTimedWithOutputMonitor

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

阻塞并执行操作,如果所需时间超过指定时间,则取消。它还会监控输出流中的活动,如果在指定时间内未观察到任何流活动,则中止。如果将 idilOutputTimeout 值设为零,则不会发生音频流监控。

参数
timeout long:最长等待时间(毫秒)

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

runnable IRunUtil.IRunnableResult:要执行的 IRunUtil.IRunnableResult

logErrors boolean:记录异常与非异常相关的错误。

返回
CommandStatus 运算的 CommandStatus 结果。

setEnvVariable

public abstract void setEnvVariable (String key, 
                String value)

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

参数
key String:变量名称

value String:变量值

setEnvVariablePriority

public abstract void setEnvVariablePriority (IRunUtil.EnvPriority priority)

确定在创建进程时是否取消设置环境变量的优先级高于设置环境变量。默认情况下,取消设置优先级较高:这意味着,如果尝试设置具有相同名称的变量,则不会发生这种情况,因为未设置该变量。无法用于默认的“IRunUtil”实例。

参数
priority IRunUtil.EnvPriority

setInterruptibleInFuture

public abstract void setInterruptibleInFuture (Thread thread, 
                long timeMs)

在等待一段时间后设置为可中断。 ERROR(/CommandScheduler#shutdownHard()) 来强制执行,我们最终会终止。

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

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

setLinuxInterruptProcess

public abstract void setLinuxInterruptProcess (boolean interrupt)

允许在 Linux 通过 #runTimed 方法运行的进程超时时“终止”中断。 无法用于默认的“IRunUtil”实例。

参数
interrupt boolean

setRedirectStderrToStdout

public abstract void setRedirectStderrToStdout (boolean redirect)

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

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

setWORKDir

public abstract void setWorkingDir (File dir)

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

参数
dir File:工作目录

sleep

public abstract void sleep (long time)

帮助程序方法在给定时间内休眠,忽略所有异常。

参数
time long:休眠 ms。小于或等于 0 的值将被忽略

UnsetEnvVariable

public abstract void unsetEnvVariable (String key)

取消设置环境变量,以便系统命令在没有此环境变量的情况下运行。

参数
key String:变量名称