-
Bug
-
Resolution: Unresolved
-
P3
-
21
-
generic
-
generic
A DESCRIPTION OF THE PROBLEM :
The List.removeFirst method was added in Java 21 with a default implementation. This default implementation is not thread-safe, which seems appropriate.
However, the List returned by Collections.synchronizedList returns a wrapper instance over the provided List (Collections.SynchronizedList) which does not override the removeFirst method. As a consequence, when using removeFirst over the List returned by Collections.synchronizedList the default non thread-safe removeFirst will be used.
Note that the Collections.synchronizedList does not have any warning about the requirement for extra synchronisation when using removeFirst.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a multi-threaded stress test with multiple threads adding and removing elements from a list, where removal uses removeFirst.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The throw of NoSuchElementException is expected when removing over an empty list.
ACTUAL -
The OutOfBoundsException can also be thrown due to the chect-then-act present n the removeFirst.
When the test for emptiness is done, the list is NOT empty, however when the remove(0) is call the list is empty.
Note that the removeFirst does NOT include OutOfBoundsException as a possible exception.
The List.removeFirst method was added in Java 21 with a default implementation. This default implementation is not thread-safe, which seems appropriate.
However, the List returned by Collections.synchronizedList returns a wrapper instance over the provided List (Collections.SynchronizedList) which does not override the removeFirst method. As a consequence, when using removeFirst over the List returned by Collections.synchronizedList the default non thread-safe removeFirst will be used.
Note that the Collections.synchronizedList does not have any warning about the requirement for extra synchronisation when using removeFirst.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a multi-threaded stress test with multiple threads adding and removing elements from a list, where removal uses removeFirst.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The throw of NoSuchElementException is expected when removing over an empty list.
ACTUAL -
The OutOfBoundsException can also be thrown due to the chect-then-act present n the removeFirst.
When the test for emptiness is done, the list is NOT empty, however when the remove(0) is call the list is empty.
Note that the removeFirst does NOT include OutOfBoundsException as a possible exception.