Commit 2f4431be authored by lihao's avatar lihao

添加超时时间

parent a80d9858
...@@ -22,14 +22,14 @@ public class TaskExecutor<T, U, R> { ...@@ -22,14 +22,14 @@ public class TaskExecutor<T, U, R> {
private CountDownLatch countDownLatch; private CountDownLatch countDownLatch;
public TaskExecutor() { private Long countDownTimeOut;
int i = Runtime.getRuntime().availableProcessors();
int core = Math.min(i / 2, 8); private TimeUnit timeUnit;
this.pool = new ThreadPoolExecutor(core, core + 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
}
public TaskExecutor(ThreadPoolExecutor pool) { public TaskExecutor(ThreadPoolExecutor pool, Long countDownTimeOut, TimeUnit timeUnit) {
this.pool = pool; this.pool = pool;
this.countDownTimeOut = countDownTimeOut;
this.timeUnit = timeUnit;
} }
...@@ -62,13 +62,13 @@ public class TaskExecutor<T, U, R> { ...@@ -62,13 +62,13 @@ public class TaskExecutor<T, U, R> {
} }
})); }));
try { try {
countDownLatch.await(); countDownLatch.await(countDownTimeOut, timeUnit);
return failCount.get() <= 0;
} catch (InterruptedException e) { } catch (InterruptedException e) {
return false;
} finally { } finally {
this.destroy(); this.destroy();
} }
return failCount.get() <= 0;
} }
/** /**
......
package com.mushiny.task; package com.mushiny.task;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/** /**
* @author lihao * @author lihao
...@@ -11,10 +13,15 @@ public class TaskPoolFactory { ...@@ -11,10 +13,15 @@ public class TaskPoolFactory {
} }
public static <T, U, R> TaskExecutor<T, U, R> createExecutor() { 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) { 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