Commit 2f4431be authored by lihao's avatar lihao

添加超时时间

parent a80d9858
......@@ -22,14 +22,14 @@ public class TaskExecutor<T, U, R> {
private CountDownLatch countDownLatch;
public TaskExecutor() {
int i = Runtime.getRuntime().availableProcessors();
int core = Math.min(i / 2, 8);
this.pool = new ThreadPoolExecutor(core, core + 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
}
private Long countDownTimeOut;
private TimeUnit timeUnit;
public TaskExecutor(ThreadPoolExecutor pool) {
public TaskExecutor(ThreadPoolExecutor pool, Long countDownTimeOut, TimeUnit timeUnit) {
this.pool = pool;
this.countDownTimeOut = countDownTimeOut;
this.timeUnit = timeUnit;
}
......@@ -62,13 +62,13 @@ public class TaskExecutor<T, U, R> {
}
}));
try {
countDownLatch.await();
countDownLatch.await(countDownTimeOut, timeUnit);
return failCount.get() <= 0;
} catch (InterruptedException e) {
return false;
} finally {
this.destroy();
}
return failCount.get() <= 0;
}
/**
......
package com.mushiny.task;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author lihao
......@@ -11,10 +13,15 @@ public class TaskPoolFactory {
}
public static <T, U, R> TaskExecutor<T, U, R> createExecutor() {
return new TaskExecutor<>();
return createExecutor(new ThreadPoolExecutor(2, 3, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>()), 15L, TimeUnit.SECONDS);
}
public static <T, U, R> TaskExecutor<T, U, R> createExecutor(ThreadPoolExecutor pool) {
return new TaskExecutor<>(pool);
return createExecutor(pool, 15L, TimeUnit.SECONDS);
}
public static <T, U, R> TaskExecutor<T, U, R> createExecutor(ThreadPoolExecutor pool, Long timeOut, TimeUnit timeUnit) {
return new TaskExecutor<>(pool, timeOut, timeUnit);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment