Add BasicThreadFactory.builder() and deprecate

BasicThreadFactory.Builder()

Add BasicThreadFactory.deamon()
This commit is contained in:
Gary Gregory 2024-12-18 09:53:58 -05:00
parent 82640281c8
commit 35fee6998b
3 changed files with 66 additions and 8 deletions

View File

@ -85,6 +85,8 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ArrayUtils.containsAny(int[], int...).</action>
<action type="add" dev="ggregory" due-to="asgh, Gary Gregory">Add CalendarUtils.toLocalDate() #725.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add SystemUtils.IS_OS_MAC_OSX_SEQUOIA.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BasicThreadFactory.builder() and deprecate BasicThreadFactory.Builder().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BasicThreadFactory.daemon().</action>
<!-- UPDATE -->
<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>
<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>

View File

@ -119,7 +119,10 @@ public class BasicThreadFactory implements ThreadFactory {
/**
* Constructs a new instance.
*
* @deprecated Use {@link BasicThreadFactory#builder()}.
*/
@Deprecated
public Builder() {
// empty
}
@ -138,6 +141,16 @@ public class BasicThreadFactory implements ThreadFactory {
return factory;
}
/**
* Sets the daemon flag for the new {@link BasicThreadFactory} to {@code true} causing a new thread factory to create daemon threads.
*
* @return a reference to this {@link Builder}
* @since 3.18.0
*/
public Builder daemon() {
return daemon(true);
}
/**
* Sets the daemon flag for the new {@link BasicThreadFactory}. If this
* flag is set to <strong>true</strong> the new thread factory will create daemon
@ -221,6 +234,16 @@ public class BasicThreadFactory implements ThreadFactory {
}
}
/**
* Creates a new builder.
*
* @return a new builder.
* @since 3.18.0
*/
public static Builder builder() {
return new Builder();
}
/** A counter for the threads created by this factory. */
private final AtomicLong threadCounter;

View File

@ -53,8 +53,12 @@ public class BasicThreadFactoryTest extends AbstractLangTest {
final Thread t = new Thread();
EasyMock.expect(wrapped.newThread(r)).andReturn(t);
EasyMock.replay(wrapped, r);
final BasicThreadFactory factory = builder.wrappedFactory(wrapped).daemon(
flag).build();
// @formatter:off
final BasicThreadFactory factory = builder
.wrappedFactory(wrapped)
.daemon(flag)
.build();
// @formatter:on
assertSame(t, factory.newThread(r), "Wrong thread");
assertEquals(flag, t.isDaemon(), "Wrong daemon flag");
EasyMock.verify(wrapped, r);
@ -75,7 +79,7 @@ public class BasicThreadFactoryTest extends AbstractLangTest {
@BeforeEach
public void setUp() {
builder = new BasicThreadFactory.Builder();
builder = BasicThreadFactory.builder();
}
/**
@ -87,6 +91,26 @@ public class BasicThreadFactoryTest extends AbstractLangTest {
checkFactoryDefaults(factory);
}
/**
* Tests the daemon() method of the builder.
*/
@Test
public void testBuilderDaemon() {
builder.daemon();
assertTrue(builder.build().getDaemonFlag());
}
/**
* Tests the daemon() method of the builder.
*/
@Test
public void testBuilderDaemonBoolean() {
builder.daemon(true);
assertTrue(builder.build().getDaemonFlag());
builder.daemon(false);
assertFalse(builder.build().getDaemonFlag());
}
/**
* Tests the reset() method of the builder.
*/
@ -96,9 +120,13 @@ public class BasicThreadFactoryTest extends AbstractLangTest {
final Thread.UncaughtExceptionHandler exHandler = EasyMock
.createMock(Thread.UncaughtExceptionHandler.class);
EasyMock.replay(wrappedFactory, exHandler);
builder.namingPattern(PATTERN).daemon(true).priority(
Thread.MAX_PRIORITY).uncaughtExceptionHandler(exHandler)
.wrappedFactory(wrappedFactory);
// @formatter:off
builder.namingPattern(PATTERN)
.daemon(true)
.priority(Thread.MAX_PRIORITY)
.uncaughtExceptionHandler(exHandler)
.wrappedFactory(wrappedFactory);
// @formatter:on
builder.reset();
final BasicThreadFactory factory = builder.build();
checkFactoryDefaults(factory);
@ -111,8 +139,13 @@ public class BasicThreadFactoryTest extends AbstractLangTest {
*/
@Test
public void testBuilderResetAfterBuild() {
builder.wrappedFactory(EasyMock.createNiceMock(ThreadFactory.class))
.namingPattern(PATTERN).daemon(true).build();
// @formatter:off
builder
.wrappedFactory(EasyMock.createNiceMock(ThreadFactory.class))
.namingPattern(PATTERN)
.daemon(true)
.build();
// @formatter:on
checkFactoryDefaults(builder.build());
}