Skip to content

Commit

Permalink
Merge pull request #388 from KamToHung/feat-387
Browse files Browse the repository at this point in the history
[ISSUE #387] Optimized ThreadPoolBuilder code
  • Loading branch information
yanhom1314 committed Jan 8, 2024
2 parents 38d2e72 + fc83642 commit 66c4fcc
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,26 +334,99 @@ public ThreadPoolBuilder waitForTasksToCompleteOnShutdown(boolean waitForTasksTo
return this;
}

/**
* @param eager true or false
* @return the ThreadPoolBuilder instance
* @deprecated use {@link #eager()} instead
*/
@Deprecated
public ThreadPoolBuilder eager(boolean eager) {
checkExecutorType();
this.eager = eager;
return this;
}

/**
* @param ordered true or false
* @return the ThreadPoolBuilder instance
* @deprecated use {@link #ordered()} instead
*/
@Deprecated
public ThreadPoolBuilder ordered(boolean ordered) {
checkExecutorType();
this.ordered = ordered;
return this;
}

/**
* @param scheduled true or false
* @return the ThreadPoolBuilder instance
* @deprecated use {@link #scheduled()} instead
*/
@Deprecated
public ThreadPoolBuilder scheduled(boolean scheduled) {
checkExecutorType();
this.scheduled = scheduled;
return this;
}

/**
* @param priority true or false
* @return the ThreadPoolBuilder instance
* @deprecated use {@link #priority()} instead
*/
@Deprecated
public ThreadPoolBuilder priority(boolean priority) {
checkExecutorType();
this.priority = priority;
return this;
}

/**
* set eager type
*
* @return the ThreadPoolBuilder instance
*/
public ThreadPoolBuilder eager() {
checkExecutorType();
this.eager = true;
return this;
}

/**
* set ordered type
*
* @return the ThreadPoolBuilder instance
*/
public ThreadPoolBuilder ordered() {
checkExecutorType();
this.ordered = true;
return this;
}

/**
* set scheduled type
*
* @return the ThreadPoolBuilder instance
*/
public ThreadPoolBuilder scheduled() {
checkExecutorType();
this.scheduled = true;
return this;
}

/**
* set priority type
*
* @return the ThreadPoolBuilder instance
*/
public ThreadPoolBuilder priority() {
checkExecutorType();
this.priority = true;
return this;
}


public ThreadPoolBuilder preStartAllCoreThreads(boolean preStartAllCoreThreads) {
this.preStartAllCoreThreads = preStartAllCoreThreads;
return this;
Expand Down Expand Up @@ -602,4 +675,15 @@ private ThreadPoolExecutor buildCommonExecutor(ThreadPoolBuilder builder) {
executor.allowCoreThreadTimeOut(builder.allowCoreThreadTimeOut);
return executor;
}

/**
* Check executor type.
*/
private void checkExecutorType() {
if (eager || ordered || scheduled || priority) {
// 抛异常
throw new IllegalArgumentException("More than one executor type is defined");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DtpExecutor eagerDtpExecutor() {
.corePoolSize(2)
.maximumPoolSize(4)
.queueCapacity(2000)
.eager(true)
.eager()
.buildDynamic();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DtpExecutor eagerDtpExecutor() {
.corePoolSize(2)
.maximumPoolSize(4)
.queueCapacity(2000)
.eager(true)
.eager()
.buildDynamic();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DtpExecutor eagerDtpExecutor() {
.corePoolSize(2)
.maximumPoolSize(4)
.queueCapacity(2000)
.eager(true)
.eager()
.buildDynamic();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DtpExecutor eagerDtpExecutor() {
.corePoolSize(2)
.maximumPoolSize(4)
.queueCapacity(2000)
.eager(true)
.eager()
.buildDynamic();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DtpExecutor eagerDtpExecutor() {
.corePoolSize(2)
.maximumPoolSize(4)
.queueCapacity(2000)
.eager(true)
.eager()
.buildDynamic();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DtpExecutor eagerDtpExecutor() {
.corePoolSize(2)
.maximumPoolSize(4)
.queueCapacity(2000)
.eager(true)
.eager()
.buildDynamic();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DtpExecutor eagerDtpExecutor() {
.corePoolSize(2)
.maximumPoolSize(4)
.queueCapacity(2000)
.eager(true)
.eager()
.buildDynamic();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DtpExecutor eagerDtpExecutor() {
.corePoolSize(2)
.maximumPoolSize(4)
.queueCapacity(2000)
.eager(true)
.eager()
.buildDynamic();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public DtpExecutor eagerDtpExecutor() {
.corePoolSize(2)
.maximumPoolSize(4)
.queueCapacity(2000)
.eager(true)
.eager()
.buildDynamic();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.dromara.dynamictp.test.core.support;

import org.dromara.dynamictp.core.support.ThreadPoolBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* @author <a href = "mailto:[email protected]">KamTo Hung</a>
*/
public class ThreadPoolBuilderTest {

@Test
void testBuildDynamic() {
Assertions.assertThrows(IllegalArgumentException.class,() -> ThreadPoolBuilder.newBuilder()
.threadPoolName("dtpExecutor1")
.priority()
.ordered()
.buildDynamic());
}

}

0 comments on commit 66c4fcc

Please sign in to comment.