diff --git a/src/changes/changes.xml b/src/changes/changes.xml index d5754b251..d268afaa4 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -85,6 +85,8 @@ The type attribute can be add,update,fix,remove. Add ArrayUtils.containsAny(int[], int...). Add CalendarUtils.toLocalDate() #725. Add SystemUtils.IS_OS_MAC_OSX_SEQUOIA. + Add BasicThreadFactory.builder() and deprecate BasicThreadFactory.Builder(). + Add BasicThreadFactory.daemon(). Bump org.apache.commons:commons-parent from 73 to 78 #1267, #1277, #1283, #1288, #1302. [site] Bump org.codehaus.mojo:taglist-maven-plugin from 3.1.0 to 3.2.1 #1300. diff --git a/src/main/java/org/apache/commons/lang3/concurrent/BasicThreadFactory.java b/src/main/java/org/apache/commons/lang3/concurrent/BasicThreadFactory.java index aa0c9e24a..ff2a5d531 100644 --- a/src/main/java/org/apache/commons/lang3/concurrent/BasicThreadFactory.java +++ b/src/main/java/org/apache/commons/lang3/concurrent/BasicThreadFactory.java @@ -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 true 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; diff --git a/src/test/java/org/apache/commons/lang3/concurrent/BasicThreadFactoryTest.java b/src/test/java/org/apache/commons/lang3/concurrent/BasicThreadFactoryTest.java index 42f945105..d75aaf12b 100644 --- a/src/test/java/org/apache/commons/lang3/concurrent/BasicThreadFactoryTest.java +++ b/src/test/java/org/apache/commons/lang3/concurrent/BasicThreadFactoryTest.java @@ -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()); }