开发者

Don't understand some behaviour of @async

开发者 https://www.devze.com 2023-01-24 22:32 出处:网络
I\'m having some difficulty understanding a certain aspect of the @async annotation, and possibly the workings of threads and threadpooling as a whole. When I set this in my spring configuration file:

I'm having some difficulty understanding a certain aspect of the @async annotation, and possibly the workings of threads and threadpooling as a whole. When I set this in my spring configuration file:

<task:executor id="WhifExecutor" pool-size="10"/>
<task:annotation-driven executor="WhifExecutor" />

wouldn't that mean only 10 threads will be started? Yet when I run a function th开发者_开发技巧at has @async a 1000 times it immediately continues with the rest of the code after the call (a simple message is shown in the console to show the number of times the function has been called). Then after a while the pooled functions start to return their values, but strangely enough only from 1 pool and 10 distinct threads. What is going on here? How can it call all those 1000 functions yet only use 10 threads? Does it maybe put all of them on some stack before starting the threads? I tried reading the documentation on this but I couldn't find anything about this phenomenon.

Also, is there a way to make it wait for threads to start running so that I don't immediately make a call to a couple thousand functions at once?


Does it maybe put all of them on some stack before starting the threads?

Not a stack, but a queue.

The executor will be allocated 10 threads. If all 10 threads are busy, and new tasks are added, they will be added to a queue, and executed in turn. No more than 10 tasks will be executed at one time.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号