On Thu, 18 Jun 2026 08:21:05 GMT, Prasanta Sadhukhan <[email protected]> wrote:
> `DTD.getElement(String)` is not thread safe. If there are 2 parallel parser > threads which call with the same new name then in the Vector there can be 2 > identical elements. These unsynchronized elementHash.get(name) calls can both > observe null before either thread creates and puts the `Element` which will > then have duplicates. The index from elements.size() can also be wrong under > races. > > The block needs to be synchronized to prevent this > > --------- > - [x] I confirm that I make this contribution in accordance with the [OpenJDK > Interim AI Policy](https://openjdk.org/legal/ai). Should `DTD.getElement` be thread-safe? Most Swing isn't thread safe, the `DTD` class does not specify it is thread-safe. If you make `DTD.getElement(String)` thread-safe, it doesn't make the entire class thread-safe, there are many cases where `elements` and `elementHash` are accessed to get data—to make the `DTD` class thread-safe, you have to modify all these cases, otherwise it does not make sense. Similarly, `DTD.getElement(int)` isn't thread-safe. Neither is `DTD.defineEntity`. Thus, it looks to me the `DTD` class wasn't designed to be thread-safe. If the class needs to be thread-safe, then making one method thread-safe doesn't resolve anything, something else may still fail if the `DTD` class is accessed concurrently. ------------- PR Comment: https://git.openjdk.org/jdk/pull/31568#issuecomment-4751036657
