Commit a84189b9 authored by lihao's avatar lihao

code review

parent 4456aee3
......@@ -8,22 +8,25 @@ import java.util.concurrent.TimeUnit;
* @author lihao
*/
public class TaskPoolFactory {
public TaskPoolFactory() {
// ignore
}
public <T, U, R> TaskExecutor<T, U, R> createExecutor() {
private ThreadPoolExecutor threadPoolExecutor;
public TaskPoolFactory() {
int core = Runtime.getRuntime().availableProcessors();
int poolSize = Math.min(core / 2, 4);
return createExecutor(new ThreadPoolExecutor(poolSize, poolSize + 1, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>()), 15L, TimeUnit.SECONDS);
this.threadPoolExecutor = new ThreadPoolExecutor(poolSize, poolSize + 1, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>());
}
public TaskPoolFactory(ThreadPoolExecutor threadPoolExecutor) {
this.threadPoolExecutor = threadPoolExecutor;
}
public <T, U, R> TaskExecutor<T, U, R> createExecutor(ThreadPoolExecutor pool) {
return createExecutor(pool, 15L, TimeUnit.SECONDS);
public <T, U, R> TaskExecutor<T, U, R> createExecutor() {
return createExecutor(15L, TimeUnit.SECONDS);
}
public <T, U, R> TaskExecutor<T, U, R> createExecutor(ThreadPoolExecutor pool, Long timeOut, TimeUnit timeUnit) {
return new TaskExecutor<>(pool, timeOut, timeUnit);
public <T, U, R> TaskExecutor<T, U, R> createExecutor(Long timeOut, TimeUnit timeUnit) {
return new TaskExecutor<>(this.threadPoolExecutor, timeOut, timeUnit);
}
}
......@@ -7,8 +7,6 @@ import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TaskTests {
......@@ -48,8 +46,7 @@ public class TaskTests {
@Test
public void multiTaskLoopWithArgsTest() {
TaskPoolFactory taskPoolFactory = new TaskPoolFactory();
TaskExecutor<User, User, String> executor = taskPoolFactory.createExecutor(new ThreadPoolExecutor(8, 9,
0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>()), 60L, TimeUnit.SECONDS);
TaskExecutor<User, User, String> executor = taskPoolFactory.createExecutor(60L, TimeUnit.SECONDS);
executor.init((data, context) -> {
data.setName(data.getName() + "handle");
......
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