From a1a8c448437c080e805ed332735f6b3376358a62 Mon Sep 17 00:00:00 2001 From: duncanmcqueen Date: Wed, 14 Oct 2026 08:26:59 -0500 Subject: [PATCH] Thread: make the thread-local map on first use Some VMs (for example Kaffe) make the Thread object of the main thread without running a constructor, so its locals field is null and every ThreadLocal fails. Make the field non-final and create the map in getThreadLocals() when it is null. Co-Authored-By: Claude Opus 5.5 Signed-off-by: duncanmcqueen --- java/lang/Thread.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/java/lang/Thread.java b/java/lang/Thread.java index bc2dbb5b70..cb903fabdd 100644 --- a/java/lang/Thread.java +++ b/java/lang/Thread.java @@ -159,7 +159,7 @@ public class Thread implements Runnable /** Thread local storage. Package accessible for use by * InheritableThreadLocal. */ - final ThreadLocalMap locals; + ThreadLocalMap locals; /** The uncaught exception handler. */ UncaughtExceptionHandler exceptionHandler; @@ -1073,7 +1073,10 @@ synchronized void die() */ static ThreadLocalMap getThreadLocals() { - return currentThread().locals; + Thread t = currentThread(); + if (t.locals == null) + t.locals = new ThreadLocalMap(); + return t.locals; } /**