From 1841ad3472706d79894c12bc1bccac5dc4a925f3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 22 Apr 2025 12:17:03 +0200 Subject: [PATCH] Consistently use local copy of volatile mainThreadPrefix field Closes gh-34746 --- .../beans/factory/support/DefaultListableBeanFactory.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 44f24cc912d..29271d5b011 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -1066,8 +1066,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @Nullable protected Boolean isCurrentThreadAllowedToHoldSingletonLock() { String mainThreadPrefix = this.mainThreadPrefix; - if (this.mainThreadPrefix != null) { - // We only differentiate in the preInstantiateSingletons phase. + if (mainThreadPrefix != null) { + // We only differentiate in the preInstantiateSingletons phase, using + // the volatile mainThreadPrefix field as an indicator for that phase. PreInstantiation preInstantiation = this.preInstantiationThread.get(); if (preInstantiation != null) { @@ -1087,7 +1088,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto } else if (this.strictLocking == null) { // No explicit locking configuration -> infer appropriate locking. - if (mainThreadPrefix != null && !getThreadNamePrefix().equals(mainThreadPrefix)) { + if (!getThreadNamePrefix().equals(mainThreadPrefix)) { // An unmanaged thread (assumed to be application-internal) with lenient locking, // and not part of the same thread pool that provided the main bootstrap thread // (excluding scenarios where we are hit by multiple external bootstrap threads).