This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new fa768e3 make threadNameCache actually useful fa768e3 is described below commit fa768e3878ae56fbdfadf3a86e1d218e722c459d Author: t-gergely <8715996+t-gerg...@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 | 60 +++++++++++++++++++++++++++ 2 files changed, 62 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..8764c0d --- /dev/null +++ b/test/org/apache/juli/TestThreadNameCache.java @@ -0,0 +1,60 @@ +/* + * 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 int 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= (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); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org