You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2019/05/01 21:30:13 UTC

[commons-lang] branch master updated: [LANG-1455] Add a DaemonThreadFactory.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new b30be60  [LANG-1455] Add a DaemonThreadFactory.</action>
b30be60 is described below

commit b30be60a81a14921b3c6bca9689f4886693f1bcd
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Wed May 1 17:30:00 2019 -0400

    [LANG-1455] Add a DaemonThreadFactory.</action>
---
 src/changes/changes.xml                            |  1 +
 .../lang3/concurrent/DaemonThreadFactory.java      | 55 ++++++++++++++++++++++
 .../lang3/concurrent/DaemonThreadFactoryTest.java  | 50 ++++++++++++++++++++
 3 files changed, 106 insertions(+)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 7ef4c18..ac0834f 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -47,6 +47,7 @@ The <action> type attribute can be add,update,fix,remove.
 
   <release version="3.10" date="YYYY-MM-DD" description="TBD">
     <action issue="LANG-1450" type="fix" dev="chtompki">Generate javadoc jar on build.</action>
+    <action issue="LANG-1455" type="add" dev="ggregory">Add a DaemonThreadFactory.</action>
   </release>
 
   <release version="3.9" date="2019-04-09" description="New features and bug fixes. Requires Java 8, supports Java 9, 10, 11">
diff --git a/src/main/java/org/apache/commons/lang3/concurrent/DaemonThreadFactory.java b/src/main/java/org/apache/commons/lang3/concurrent/DaemonThreadFactory.java
new file mode 100644
index 0000000..b8e4b17
--- /dev/null
+++ b/src/main/java/org/apache/commons/lang3/concurrent/DaemonThreadFactory.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.lang3.concurrent;
+
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * A {@link ThreadFactory} that produces daemon threads.
+ *
+ * @since 3.10
+ */
+public class DaemonThreadFactory implements ThreadFactory {
+
+    private static final String FORMAT = "%s-%s";
+    private final String namePrefix;
+    private final AtomicInteger threadCount = new AtomicInteger(0);
+
+    public DaemonThreadFactory() {
+        this(DaemonThreadFactory.class.getSimpleName());
+    }
+
+    public DaemonThreadFactory(final String prefix) {
+        this.namePrefix = prefix;
+    }
+
+    @Override
+    public Thread newThread(final Runnable runnable) {
+        if (runnable == null) {
+            return null;
+        }
+        final String name = String.format(FORMAT, this.namePrefix, this.threadCount.incrementAndGet());
+        final Thread thread = new Thread(runnable, name);
+        thread.setDaemon(true);
+        if (thread.getPriority() != Thread.NORM_PRIORITY) {
+            thread.setPriority(Thread.NORM_PRIORITY);
+        }
+        return thread;
+    }
+}
diff --git a/src/test/java/org/apache/commons/lang3/concurrent/DaemonThreadFactoryTest.java b/src/test/java/org/apache/commons/lang3/concurrent/DaemonThreadFactoryTest.java
new file mode 100644
index 0000000..07203cd
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/concurrent/DaemonThreadFactoryTest.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.lang3.concurrent;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class DaemonThreadFactoryTest {
+
+    private static Runnable NOOP_RUNNABLE = new Runnable() {
+
+        @Override
+        public void run() {
+            // noop
+
+        }
+    };
+
+    @Test
+    public void testThreadFactory() {
+        final Thread thread = new DaemonThreadFactory().newThread(NOOP_RUNNABLE);
+        Assertions.assertTrue(thread.isDaemon());
+        final String name = thread.getName();
+        Assertions.assertTrue(name.startsWith("DaemonThreadFactory-"), name);
+    }
+
+    @Test
+    public void testThreadFactoryPrefix() {
+        final String expectedName = DaemonThreadFactoryTest.class.getSimpleName();
+        final Thread thread = new DaemonThreadFactory(expectedName).newThread(NOOP_RUNNABLE);
+        Assertions.assertTrue(thread.isDaemon());
+        final String name = thread.getName();
+        Assertions.assertTrue(name.startsWith(expectedName + "-"), name);
+    }
+}