日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java超时结束程序_java本机进程超时

發布時間:2024/9/27 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java超时结束程序_java本机进程超时 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

java本機進程超時

目前,我使用以下命令執行本機進程:

java.lang.Process process = Runtime.getRuntime().exec(command);

int returnCode = process.waitFor();

假設我希望在經過一定時間后終止,而不是等待程序返回。 我該怎么做呢?

deltanovember asked 2020-07-22T02:28:34Z

6個解決方案

51 votes

所有其他響應都是正確的,但是可以使用FutureTask使它更健壯和有效。

例如,

private static final ExecutorService THREAD_POOL

= Executors.newCachedThreadPool();

private static T timedCall(Callable c, long timeout, TimeUnit timeUnit)

throws InterruptedException, ExecutionException, TimeoutException

{

FutureTask task = new FutureTask(c);

THREAD_POOL.execute(task);

return task.get(timeout, timeUnit);

}

try {

int returnCode = timedCall(new Callable() {

public Integer call() throws Exception {

java.lang.Process process = Runtime.getRuntime().exec(command);

return process.waitFor();

}

}, timeout, TimeUnit.SECONDS);

} catch (TimeoutException e) {

// Handle timeout here

}

如果重復執行此操作,則線程池會更高效,因為它會緩存線程。

ZZ Coder answered 2020-07-22T02:30:06Z

21 votes

如果您使用的是Java 8,則可以將新的waitFor與超時一起使用:

Process p = ...

if(!p.waitFor(1, TimeUnit.MINUTE)) {

//timeout - kill the process.

p.destroy(); // consider using destroyForcibly instead

}

Aleksander Blomsk?ld answered 2020-07-22T02:30:26Z

19 votes

這是Plexus CommandlineUtils的工作方式:

Process p;

p = cl.execute();

...

if ( timeoutInSeconds <= 0 )

{

returnValue = p.waitFor();

}

else

{

long now = System.currentTimeMillis();

long timeoutInMillis = 1000L * timeoutInSeconds;

long finish = now + timeoutInMillis;

while ( isAlive( p ) && ( System.currentTimeMillis() < finish ) )

{

Thread.sleep( 10 );

}

if ( isAlive( p ) )

{

throw new InterruptedException( "Process timeout out after " + timeoutInSeconds + " seconds" );

}

returnValue = p.exitValue();

}

public static boolean isAlive( Process p ) {

try

{

p.exitValue();

return false;

} catch (IllegalThreadStateException e) {

return true;

}

}

Rich Seller answered 2020-07-22T02:29:37Z

6 votes

那Groovy的方式呢

public void yourMethod() {

...

Process process = new ProcessBuilder(...).start();

//wait 5 secs or kill the process

waitForOrKill(process, TimeUnit.SECONDS.toMillis(5));

...

}

public static void waitForOrKill(Process self, long numberOfMillis) {

ProcessRunner runnable = new ProcessRunner(self);

Thread thread = new Thread(runnable);

thread.start();

runnable.waitForOrKill(numberOfMillis);

}

protected static class ProcessRunner implements Runnable {

Process process;

private boolean finished;

public ProcessRunner(Process process) {

this.process = process;

}

public void run() {

try {

process.waitFor();

} catch (InterruptedException e) {

// Ignore

}

synchronized (this) {

notifyAll();

finished = true;

}

}

public synchronized void waitForOrKill(long millis) {

if (!finished) {

try {

wait(millis);

} catch (InterruptedException e) {

// Ignore

}

if (!finished) {

process.destroy();

}

}

}

}

mickthompson answered 2020-07-22T02:30:46Z

4 votes

只是根據我的要求做了一些修改。 這里的超時時間是10秒。 如果該進程沒有退出,則會在10秒后被銷毀。

public static void main(String arg[]) {

try {

Process p = Runtime.getRuntime().exec("\"C:/Program Files/VanDyke Software/SecureCRT/SecureCRT.exe\"");

long now = System.currentTimeMillis();

long timeoutInMillis = 1000L * 10;

long finish = now + timeoutInMillis;

while ( isAlive( p ) ) {

Thread.sleep( 10 );

if ( System.currentTimeMillis() > finish ) {

p.destroy();

}

}

} catch (Exception err) {

err.printStackTrace();

}

}

public static boolean isAlive( Process p ) {

try {

p.exitValue();

return false;

} catch (IllegalThreadStateException e) {

return true;

}

}

nuwan answered 2020-07-22T02:31:06Z

2 votes

您需要一個2.線程來中斷調用.waitFor()的線程。為了使其健壯,將需要一些非同尋常的同步,但基礎知識是:

TimeoutThread:

Thread.sleep(timeout);

processThread.interrupt();

ProcessThread:

try {

proc.waitFor();

} catch (InterruptedException e) {

proc.destroy();

}

nos answered 2020-07-22T02:31:35Z

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java超时结束程序_java本机进程超时的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。