RunUtil

public class RunUtil
extends Object implements IRunUtil

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


執行作業的輔助方法集合。

摘要

欄位

public static final String INHERITIO_PREFIX

public static final 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)

可接受 ERROR(/List) 格式指令引數的替代 runCmdInBackground(String) 方法。

Process runCmdInBackground( command)

可接受 ERROR(/List) 格式指令引數的替代 runCmdInBackground(String) 方法。

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)

允許在達到逾時時,使用 Linux 的「kill」中斷功能,對透過 #runTimed 方法執行的程序進行中斷。

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

RUNNABLE_NOTIFIER_NAME

public static final String RUNNABLE_NOTIFIER_NAME

公用建構函式

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 StringRunInterruptedException 的訊息。

errorId ErrorIdentifier:代表中斷原因 (如果已知)。

中斷

public void interrupt (Thread thread, 
                String message)

中斷指定執行緒上正在執行/即將執行的作業。在指定執行緒上執行的作業會擲回 RunInterruptedException

參數
message StringRunInterruptedException 的訊息。

isInterruptAllowed

public boolean isInterruptAllowed ()

提供 RunUtil 的中斷狀態。

傳回
boolean 如果可中斷執行作業,則傳回「是」;否則傳回「否」。

runCmdInBackground

public Process runCmdInBackground (Redirect redirect, 
                 command)

可接受 ERROR(/List) 格式指令引數的替代 runCmdInBackground(String) 方法。

參數
redirect Redirect:要套用至 ProcessBuilderERROR(/Redirect)

command :包含指定系統指令和選用引數的 ERROR(/List),用於執行

傳回
Process 已執行指令的 Process

runCmdInBackground

public Process runCmdInBackground ( command)

可接受 ERROR(/List) 格式指令引數的替代 runCmdInBackground(String) 方法。

參數
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.IRunnableResult:要執行的 IRunUtil.IRunnableResult

傳回
boolean true:如果作業在 maxTime 到期前順利完成

runFixedTimedRetry

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

封鎖並多次執行作業,直到成功為止。

參數
opTimeout long:單一作業嘗試的最大等待時間 (以毫秒為單位)

pollInterval long:作業嘗試之間的初始等待時間

maxTime long:持續嘗試作業的總預估時間上限

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

傳回
boolean true:如果作業在 maxTime 到期前順利完成

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.IRunnableResult:要執行的 IRunUtil.IRunnableResult

傳回
boolean true:如果作業在 maxTime 到期前順利完成

runTimed

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

封鎖並執行作業,如果作業時間超過指定時間,則中止作業。

參數
timeout long:等待時間上限 (以毫秒為單位)

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

logErrors boolean:是否記錄例外狀況的錯誤。

傳回
CommandStatus 運算的 CommandStatus 結果。

runTimedCmd

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

輔助方法可執行系統指令,如果執行時間超過指定時間,則中止執行,並將輸出內容重新導向至檔案 (如果指定)。以這種方式提供 ERROR(/OutputStream) 時,會在函式結束時將其保持開啟狀態。

參數
timeout long:逾時最大等待時間 (以毫秒為單位)。0 表示沒有逾時。

stdout OutputStreamERROR(/OutputStream),其中 std 輸出內容會重新導向。可為空值。

stderr OutputStreamERROR(/OutputStream) 會將錯誤輸出內容重新導向。可為空值。

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 FileERROR(/File),其中 std 輸出內容會重新導向。可為空值。

stderrFile FileERROR(/File) 會將錯誤輸出內容重新導向。可為空值。

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,則不會重新導向 stdin。

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 OutputStreamERROR(/OutputStream),其中 std 輸出內容會重新導向。可為空值。

stderr OutputStreamERROR(/OutputStream) 會將錯誤輸出內容重新導向。可為空值。

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.IRunnableResult:要執行的 IRunUtil.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.IRunnableResult:要執行的 IRunUtil.IRunnableResult

傳回
boolean 如果在嘗試次數達到上限之前,作業已順利完成,則為 true

runTimedWithOutputMonitor

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

封鎖並執行作業,如果作業時間超過指定時間,則中止作業。也會監控輸出串流的活動,如果在指定時間內未偵測到任何串流活動,就會中止。如果將 idleOutputTimeout 設為零,系統就不會監控串流。

參數
timeout long:等待時間上限 (以毫秒為單位)

idleOutputTimeout long:輸出串流的輸出內容等待時間上限 (以毫秒為單位)

runnable IRunUtil.IRunnableResult:要執行的 IRunUtil.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)

允許在達到逾時時,使用 Linux 的「kill」中斷功能,對透過 #runTimed 方法執行的程序進行中斷。無法在預設 IRunUtil 例項上使用。

參數
interrupt boolean

setRedirectStderrToStdout

public void setRedirectStderrToStdout (boolean redirect)

設定標準錯誤串流,以便在執行系統指令時重新導向至標準輸出串流。初始值為 false。

參數
redirect boolean:是否重新導向的新值

setWorkingDir

public void setWorkingDir (File dir)

設定系統指令的工作目錄。

參數
dir File:工作目錄

舒眠

public void sleep (long time)

輔助方法,可讓程式暫停一段時間,並忽略任何例外狀況。

參數
time long:休眠時間 (以毫秒為單位)。系統會忽略小於或等於 0 的值

unsetEnvVariable

public void unsetEnvVariable (String key)

取消設定環境變數,讓系統指令在沒有這個環境變數的情況下執行。環境變數可能會從父程序繼承,因此我們需要從 ProcessBuilder.environment() 刪除環境變數

參數
key String:變數名稱