Commit a84189b9 authored by lihao's avatar lihao

code review

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