Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #387] Optimized ThreadPoolBuilder code #388

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}

}