Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide hints for conversion to Virtual thread executor when thread pools are used #4592

Merged
merged 7 commits into from
Oct 18, 2022

Conversation

MegJayan
Copy link
Contributor

@MegJayan MegJayan commented Sep 7, 2022

Provide hints for converting thread pools(newFixedThreadPool, newCachedThreadPool) to virtual thread pool executor.
When ThreadFactory is passed as argument to , then only a hint suggestion for converting the thread pool to newThreadPerTaskExecutor is provided. Code is not directly rewritten in this case.

@mbien mbien added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) hints labels Sep 12, 2022
Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few comments on where I think the hint could be improved. But, I wonder if "blindly" suggesting a conversion to newVirtualThreadPerTaskExecutor is desirable - doesn't newVirtualThreadPerTaskExecutor work best for tasks that wait a lot e.g. on I/O?

Also, there are probably patterns that should not be re-written, like Executors.newFixedThreadPool(1) - that is likely to mean run on background, but single-threaded. Changing this to multiple (virtual or non-virtual) threads may lead to problems.

Copy link
Member

@mbien mbien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should discuss if this is something the IDE should recommend by default. Because switching to VTs is not something which should be done blindly.

Even though the API is the same, the concepts are not. E.g there usually is no reason to migrate purely CPU bound tasks (as correctly mentioned in the hint description).

But more importantly: fixed thread pools are not the same as a newVirtualThreadPerTaskExecutor, you going to need a semaphore to achieve similar behavior otherwise you just added a DDOS vuln. There are also other things to pay attention to (in both your code and in libraries), e.g usage of ThreadLocals which used to be an optimization, but are now an anti-pattern which doesn't scale anymore. Or synchronized sections which can block or native stack frames which could lock carrier threads etc.

VTs are great and I am all for using new features and having code transformations which help with migration but maybe not enabled by default :)

options to consider: disable the hint, or make it current-line only

@neilcsmith-net @matthiasblaesing whats your opinions on this?

@neilcsmith-net
Copy link
Member

VTs are great and I am all for using new features and having code transformations which help with migration but maybe not enabled by default

Emphasis mine, but yes, agree, IMO should not be enabled by default, for now at least.

@arvindaprameya
Copy link

Based on the comments we have now changed to give hints for the new feature of VT thats available.
Instead of converting the code. So no more automatic conversion.Hope this clarifies.

Copy link
Member

@mbien mbien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great addition - helps to find VT candidates

@singh-akhilesh singh-akhilesh merged commit b696342 into apache:master Oct 18, 2022
@neilcsmith-net
Copy link
Member

@singh-akhilesh please make sure there's a milestone set when merging, thanks!

@neilcsmith-net neilcsmith-net added this to the NB16 milestone Oct 18, 2022
Chris2011 pushed a commit to Chris2011/netbeans that referenced this pull request Dec 15, 2022
…ools are used (apache#4592)

* add hints for using virtual threads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hints Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants