import org.gradle.api.GradleException
import java.util.concurrent.*
/**
15/08/2017
耗时操作
*/
class TimeConsuming {
static final ExecutorService EXECUTOR = Executors.newCachedThreadPool()
/**
* taskName 任务名称
* timeoutInSecond 超时时间
*/
static task = { def taskName, int timeoutInSecond = 60, Closure taskToRun ->
final Future<?> future = EXECUTOR.submit(new Runnable() {
@Override
void run() {
taskToRun.call()
}
})
try {
future.get(timeoutInSecond, TimeUnit.SECONDS)
} catch (TimeoutException e1) {
e1.printStackTrace()
throw new GradleException("${taskName}>>>操作超时,timeout=${timeoutInSecond} second")
} catch (Exception e2) {
e2.printStackTrace()
throw new GradleException("${taskName}>>>操作异常")
}
}
static def taskWithResult (def taskName, int timeoutInSecond = 60, Closure taskToRun){
final Future future = EXECUTOR.submit(new Callable() {
@Override
Object call() throws Exception {
return taskToRun.call()
}
})
try {
return future.get(timeoutInSecond, TimeUnit.SECONDS)
}catch (TimeoutException e1) {
e1.printStackTrace()
throw new GradleException("${taskName}>>>操作超时,timeout=${timeoutInSecond} second")
} catch (Exception e2) {
e2.printStackTrace()
throw new GradleException("${taskName}>>>操作异常")
}
}
}
自己写的 groovy 处理耗时操作的一个类
猜你喜欢
转载自blog.csdn.net/lonewolf521125/article/details/79543512
今日推荐
周排行