On Mon, 15 Jun 2026 17:26:59 GMT, Jaikiran Pai <[email protected]> wrote:

>> src/java.base/share/classes/java/lang/Object.java line 314:
>> 
>>> 312:      *      <div class="preview-comment">
>>> 313:      *          If this object is a {@linkplain 
>>> java.util.Objects#hasIdentity value object},
>>> 314:      *          it does does not have a monitor, an {@code 
>>> IllegalMonitorStateException} is thrown.
>> 
>> Suggestion:
>> 
>>      *          it does not have a monitor, an {@code 
>> IllegalMonitorStateException} is thrown.
>
> Several places in the API specifications, specify that:
> 
>> Use of value class instances for synchronization, mutexes, or with
>> {@linkplain java.lang.ref.Reference object references} results in
>> {@link IdentityException}
> 
> whereas here, in these wait(), notify() etc... APIs, which are closer to 
> synchronization semantics, a `IllegalMonitorStateException` gets thrown for 
> value objects. Is there a reason why we don't throw a `IdentityException` 
> from these APIs too? I haven't followed Valhalla development, so if this was 
> already discussed, please point me to it.

While at it, I did a quick experiment with value object:


jshell> Object o = Integer.parseInt("42")
o ==> 42

jshell> o.wait()
|  Exception java.lang.IllegalMonitorStateException: java.lang.Integer
|        at Object.wait0 (Native Method)
|        at Object.wait (Object.java:433)
|        at Object.wait (Object.java:387)
|        at (#15:1)


So the `IllegalMonitorStateException` thrown merely states the class name of 
the object but doesn't tell anything more. Whereas for a identity object (like 
a String), it throws:


jshell> Object s = new String("")
s ==> ""

jshell> s.wait()
|  Exception java.lang.IllegalMonitorStateException: current thread is not owner
|        at Object.wait0 (Native Method)
|        at Object.wait (Object.java:433)
|        at Object.wait (Object.java:387)
|        at (#17:1)

is a bit more informative why it's an illegal monitor state.

Could the IllegalMonitorStateException exception message for value objects be 
improved to state something like:


Exception java.lang.IllegalMonitorStateException: instance of java.lang.Integer 
value class

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/31123#discussion_r3415375403

Reply via email to