3 ms·
One question: virtual threads are lightweight, but are the executors which create them? That is, should i organise my code so i create a single executor and us
by twic 5y ago
One question: virtual threads are lightweight, but are the executors which create them?
That is, should i organise my code so i create a single executor and use it across many operations, or is it okay to create and destroy executors all over the place?
And are the executors threadsafe - both for use from virtual threads, and from native ones?
As an aside, these are things which is important to know, but which are very rarely documented. We had some headaches a while ago because we wrote code which treated the new HttpClient as lightweight when it is very much not.
- kjeetgill 5y agoThe heaviest part of executors are the task queues and the threads themselves. If you're using 2 or 3 executors over a single one, you're presumably using the same amount of space in the queue and number of threads, so the "weight" doesn't change except that the queues are concurrent data structures so can be sources of contention ... in really odd hyperactive scenarios. Executors tend to be units of management (await()/shutdown()) etc. and monitoring, so that'll be your main reason to use more than just one for everything.