On Wed, 24 Jun 2026 08:15:04 GMT, Alan Bateman <[email protected]> wrote:
>> src/java.base/share/classes/java/lang/reflect/Field.java line 493:
>>
>>> 491: * @throws ExceptionInInitializerError if the initialization
>>> provoked
>>> 492: * by this method fails.
>>> 493: * @throws IllegalStateException if the current thread is
>>> initializing the
>>
>> Hello Alan, is the mention of "current thread" intentional here? The case
>> I've in mind is something like:
>>
>>
>> // get an uninitialized class
>> final Class k = Class.forName("Foo", false, someClassLoader);
>> // get hold of the field of interest
>> final Field f = k.getDeclaredField("someStaticStrictInitField");
>>
>> // trigger class initialization in Thread T1
>> Thread t1 = () -> {doSomethingToTriggerClassInit(k);}
>> t1.start();
>>
>> // access the (static) strict init field in thread T2, expect it to fail
>> with IllegalStateException, because some other thread is initializing the
>> class currently
>> Thread t2 = () -> {f.get(null);}
>> t2.start();
>>
>>
>>
>> I think this scenario where some other thread calls `Field.get()` when the
>> field's `Class` is being initialized is possible, isn't it?
>
>> II think this scenario where some other thread calls Field.get() when the
>> field's Class is being initialized is possible, isn't it?
>
> The thread executing the class initializer can freely access the class fields
> (or methods). Attempts by other threads to access the class fields (or
> methods) will block until the class is initialized. With strictly-initialized
> fields, the field must be initialized before it can be read. For
> strictly-initialized static fields, if the thread initializing the class
> attempts to read the field before it has been initialized then the getstatic
> will throw ISE. The reflective APIs to the same. So yes, "current thread" is
> intentional here.
> Attempts by other threads to access the class fields (or methods) will block
> until the class is initialized.
> ...
> The reflective APIs to the same.
I wasn't aware of this behaviour of reflective APIs (and hadn't experimented
either). Thank you.
-------------
PR Review Comment:
https://git.openjdk.org/valhalla/pull/2572#discussion_r3465589901