Skip to content

Found the reason #1

@alexander-shustanov

Description

@alexander-shustanov

Hi! Thank you for your highlight. But it sounds like I found a mistake in your code.

Take another look at your synchronized block. It captures the monitor of the lock object. synchronized guarantees that the code inside the block will be executed by only one thread at a time, from start to finish.

Therefore, regardless of pinning, each Thread.sleep() is executed exclusively.

However, if you capture a different monitor each time (i.e., use a different lock object per task), you'll notice a significant difference — the performance can improve by an order of magnitude.

Please, look at my fixes there:
https://github.com/spring-aio/java24-pinning/blob/master/src/main/java/dev/danvega/Application.java#L43

I create new lock every time. So, for java 21, it works the same as in your code, due to carrier thread pinning (34.688 seconds for me). But for java 24 there is no pinning and the code is executed immediately (0.633 seconds for me).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions