Skip to content

Commit 8fb5e7e

Browse files
committed
minor optimizations
1 parent 11483bb commit 8fb5e7e

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcRootLogger.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,24 @@ class BigQueryJdbcRootLogger {
4949
private static Path currentLogPath = null;
5050
private static int fileCounter = 0;
5151

52-
static final long PROCESS_ID =
53-
Long.parseLong(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);
52+
static final String PROCESS_ID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
53+
54+
private static final ThreadLocal<SimpleDateFormat> DATE_FORMATTER =
55+
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
5456

5557
static String getThreadName(long threadId) {
5658
Thread current = Thread.currentThread();
5759
if (current.getId() == threadId) {
5860
return current.getName();
5961
}
60-
int count = Thread.activeCount();
62+
ThreadGroup rootGroup = current.getThreadGroup();
63+
while (rootGroup.getParent() != null) {
64+
rootGroup = rootGroup.getParent();
65+
}
66+
67+
int count = rootGroup.activeCount();
6168
Thread[] threads = new Thread[count * 2];
62-
int actualCount = Thread.enumerate(threads);
69+
int actualCount = rootGroup.enumerate(threads);
6370
for (int i = 0; i < actualCount; i++) {
6471
if (threads[i].getId() == threadId) {
6572
return threads[i].getName();
@@ -81,13 +88,11 @@ static String getThreadName(long threadId) {
8188

8289
public static Formatter getFormatter() {
8390
return new Formatter() {
84-
private final ThreadLocal<SimpleDateFormat> dateFormatter =
85-
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"));
8691
private static final int MAX_THREAD_NAME_LENGTH = 15;
8792

8893
@Override
8994
public String format(LogRecord record) {
90-
String date = dateFormatter.get().format(new Date(record.getMillis()));
95+
String date = DATE_FORMATTER.get().format(new Date(record.getMillis()));
9196

9297
long threadId = record.getThreadID();
9398
String threadName = getThreadName(threadId);

0 commit comments

Comments
 (0)