Skip to content

Suppliers.memoize() thread pinning #7140

Open
@steven-sheehy

Description

API(s)

Suppliers.memoize()

How do you want it to be improved?

Change it from synchronized to ReentrantLock

Why do we need it to be improved?

Suppliers.memoize() internally uses the double-checked locking idiom with the synchronized keyword. Using synchronized with virtual threads can cause thread pinning and slow down performance.

Example

Suppliers.memoize(() -> someLongRunningTask());

Current Behavior

Functions properly but causes performance degradation when used with virtual threads on Java 21

Desired Behavior

Performs optimally with virtual threads.

Concrete Use Cases

In our virtual thread enabled REST API, added lazy loading using Suppliers.memoize() around some of our database interactions to improve performance but ended up seeing worse performance than going directly to database.

Checklist

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions