Skip to content

Commit 35fee69

Browse files
committed
Add BasicThreadFactory.builder() and deprecate
BasicThreadFactory.Builder() Add BasicThreadFactory.deamon()
1 parent 8264028 commit 35fee69

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ The <action> type attribute can be add,update,fix,remove.
8585
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ArrayUtils.containsAny(int[], int...).</action>
8686
<action type="add" dev="ggregory" due-to="asgh, Gary Gregory">Add CalendarUtils.toLocalDate() #725.</action>
8787
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_OS_MAC_OSX_SEQUOIA.</action>
88+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BasicThreadFactory.builder() and deprecate BasicThreadFactory.Builder().</action>
89+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BasicThreadFactory.daemon().</action>
8890
<!-- UPDATE -->
8991
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 73 to 78 #1267, #1277, #1283, #1288, #1302.</action>
9092
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">[site] Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300.</action>

src/main/java/org/apache/commons/lang3/concurrent/BasicThreadFactory.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ public static class Builder implements org.apache.commons.lang3.builder.Builder<
119119

120120
/**
121121
* Constructs a new instance.
122+
*
123+
* @deprecated Use {@link BasicThreadFactory#builder()}.
122124
*/
125+
@Deprecated
123126
public Builder() {
124127
// empty
125128
}
@@ -138,6 +141,16 @@ public BasicThreadFactory build() {
138141
return factory;
139142
}
140143

144+
/**
145+
* Sets the daemon flag for the new {@link BasicThreadFactory} to {@code true} causing a new thread factory to create daemon threads.
146+
*
147+
* @return a reference to this {@link Builder}
148+
* @since 3.18.0
149+
*/
150+
public Builder daemon() {
151+
return daemon(true);
152+
}
153+
141154
/**
142155
* Sets the daemon flag for the new {@link BasicThreadFactory}. If this
143156
* flag is set to <strong>true</strong> the new thread factory will create daemon
@@ -221,6 +234,16 @@ public Builder wrappedFactory(final ThreadFactory factory) {
221234
}
222235
}
223236

237+
/**
238+
* Creates a new builder.
239+
*
240+
* @return a new builder.
241+
* @since 3.18.0
242+
*/
243+
public static Builder builder() {
244+
return new Builder();
245+
}
246+
224247
/** A counter for the threads created by this factory. */
225248
private final AtomicLong threadCounter;
226249

src/test/java/org/apache/commons/lang3/concurrent/BasicThreadFactoryTest.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ private void checkDaemonFlag(final boolean flag) {
5353
final Thread t = new Thread();
5454
EasyMock.expect(wrapped.newThread(r)).andReturn(t);
5555
EasyMock.replay(wrapped, r);
56-
final BasicThreadFactory factory = builder.wrappedFactory(wrapped).daemon(
57-
flag).build();
56+
// @formatter:off
57+
final BasicThreadFactory factory = builder
58+
.wrappedFactory(wrapped)
59+
.daemon(flag)
60+
.build();
61+
// @formatter:on
5862
assertSame(t, factory.newThread(r), "Wrong thread");
5963
assertEquals(flag, t.isDaemon(), "Wrong daemon flag");
6064
EasyMock.verify(wrapped, r);
@@ -75,7 +79,7 @@ private void checkFactoryDefaults(final BasicThreadFactory factory) {
7579

7680
@BeforeEach
7781
public void setUp() {
78-
builder = new BasicThreadFactory.Builder();
82+
builder = BasicThreadFactory.builder();
7983
}
8084

8185
/**
@@ -87,6 +91,26 @@ public void testBuildDefaults() {
8791
checkFactoryDefaults(factory);
8892
}
8993

94+
/**
95+
* Tests the daemon() method of the builder.
96+
*/
97+
@Test
98+
public void testBuilderDaemon() {
99+
builder.daemon();
100+
assertTrue(builder.build().getDaemonFlag());
101+
}
102+
103+
/**
104+
* Tests the daemon() method of the builder.
105+
*/
106+
@Test
107+
public void testBuilderDaemonBoolean() {
108+
builder.daemon(true);
109+
assertTrue(builder.build().getDaemonFlag());
110+
builder.daemon(false);
111+
assertFalse(builder.build().getDaemonFlag());
112+
}
113+
90114
/**
91115
* Tests the reset() method of the builder.
92116
*/
@@ -96,9 +120,13 @@ public void testBuilderReset() {
96120
final Thread.UncaughtExceptionHandler exHandler = EasyMock
97121
.createMock(Thread.UncaughtExceptionHandler.class);
98122
EasyMock.replay(wrappedFactory, exHandler);
99-
builder.namingPattern(PATTERN).daemon(true).priority(
100-
Thread.MAX_PRIORITY).uncaughtExceptionHandler(exHandler)
101-
.wrappedFactory(wrappedFactory);
123+
// @formatter:off
124+
builder.namingPattern(PATTERN)
125+
.daemon(true)
126+
.priority(Thread.MAX_PRIORITY)
127+
.uncaughtExceptionHandler(exHandler)
128+
.wrappedFactory(wrappedFactory);
129+
// @formatter:on
102130
builder.reset();
103131
final BasicThreadFactory factory = builder.build();
104132
checkFactoryDefaults(factory);
@@ -111,8 +139,13 @@ public void testBuilderReset() {
111139
*/
112140
@Test
113141
public void testBuilderResetAfterBuild() {
114-
builder.wrappedFactory(EasyMock.createNiceMock(ThreadFactory.class))
115-
.namingPattern(PATTERN).daemon(true).build();
142+
// @formatter:off
143+
builder
144+
.wrappedFactory(EasyMock.createNiceMock(ThreadFactory.class))
145+
.namingPattern(PATTERN)
146+
.daemon(true)
147+
.build();
148+
// @formatter:on
116149
checkFactoryDefaults(builder.build());
117150
}
118151

0 commit comments

Comments
 (0)