You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2021/09/28 08:13:25 UTC
[tomcat] branch 9.0.x updated: make threadNameCache actually useful
This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push:
new 3dfe108 make threadNameCache actually useful
3dfe108 is described below
commit 3dfe1087dc7d4825ba10ee65c81be328cca137cb
Author: t-gergely <87...@users.noreply.github.com>
AuthorDate: Wed Sep 15 12:26:28 2021 +0200
make threadNameCache actually useful
---
java/org/apache/juli/OneLineFormatter.java | 8 +---
test/org/apache/juli/TestThreadNameCache.java | 61 +++++++++++++++++++++++++++
webapps/docs/changelog.xml | 4 ++
3 files changed, 67 insertions(+), 6 deletions(-)
diff --git a/java/org/apache/juli/OneLineFormatter.java b/java/org/apache/juli/OneLineFormatter.java
index ba340bd..3f1e4f6 100644
--- a/java/org/apache/juli/OneLineFormatter.java
+++ b/java/org/apache/juli/OneLineFormatter.java
@@ -43,7 +43,7 @@ public class OneLineFormatter extends Formatter {
private static final Object threadMxBeanLock = new Object();
private static volatile ThreadMXBean threadMxBean = null;
private static final int THREAD_NAME_CACHE_SIZE = 10000;
- private static ThreadLocal<ThreadNameCache> threadNameCache =
+ private static final ThreadLocal<ThreadNameCache> threadNameCache =
ThreadLocal.withInitial(() -> new ThreadNameCache(THREAD_NAME_CACHE_SIZE));
/* Timestamp format */
@@ -221,11 +221,7 @@ public class OneLineFormatter extends Formatter {
*/
private static String getThreadName(int logRecordThreadId) {
Map<Integer,String> cache = threadNameCache.get();
- String result = null;
-
- if (logRecordThreadId > (Integer.MAX_VALUE / 2)) {
- result = cache.get(Integer.valueOf(logRecordThreadId));
- }
+ String result = cache.get(Integer.valueOf(logRecordThreadId));
if (result != null) {
return result;
diff --git a/test/org/apache/juli/TestThreadNameCache.java b/test/org/apache/juli/TestThreadNameCache.java
new file mode 100644
index 0000000..d32efb81
--- /dev/null
+++ b/test/org/apache/juli/TestThreadNameCache.java
@@ -0,0 +1,61 @@
+/*
+ * 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.juli;
+
+import java.lang.reflect.Method;
+import java.util.concurrent.CountDownLatch;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestThreadNameCache {
+ private Integer threadId;
+
+ @Test
+ public void testCache() throws Exception {
+ final String THREAD_NAME = "t-TestThreadNameCache";
+ final CountDownLatch threadIdLatch = new CountDownLatch(1);
+ final CountDownLatch cacheLatch = new CountDownLatch(1);
+
+ OneLineFormatter olf = new OneLineFormatter();
+ Method getThreadName = olf.getClass().getDeclaredMethod("getThreadName", int.class);
+ getThreadName.setAccessible(true);
+ Thread thread = new Thread() {
+ @Override
+ public void run() {
+ setName(THREAD_NAME);
+ threadId = Integer.valueOf((int) getId());
+ threadIdLatch.countDown();
+ try {
+ cacheLatch.await();
+ } catch (InterruptedException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
+ };
+
+ thread.start();
+ threadIdLatch.await();
+ Object name = getThreadName.invoke(olf, threadId);
+ cacheLatch.countDown();
+ Assert.assertEquals(THREAD_NAME, name);
+
+ thread.join();
+ name = getThreadName.invoke(olf, threadId);
+ Assert.assertEquals(THREAD_NAME, name);
+ }
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index a06329a..42f226a 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -126,6 +126,10 @@
correctly for directory lookups whether or not the provided directory
name includes the trailing <code>/</code>. (markt)
</fix>
+ <fix>
+ <pr>451</pr>: Improve the usefulness of the thread name cache used in
+ JULI. Pull request provided by t-gergely. (markt)
+ </fix>
</changelog>
</subsection>
<subsection name="Coyote">
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org