1

I have this springboot application that heavily uses threads with @Async annotation, i didn't configure the ThreadPoolTaskExecutor because from what i think, when the thread, say thread-1 finishes with the task, other task can use back this thread-1.

But from what i observed from the log, it seems that sometime the thread number goes up to thousands, something like following:

2019-07-09 01:48:59.259  INFO 12592 --- [TaskExecutor-1] .s.d.r.c.TestingService :Something is running.
             ...... //Other threads running
2019-07-09 09:48:59.259  INFO 12592 --- [TaskExecutor-3432] .s.d.r.c.TestingService :Something is running.

I suspect none of those threads are being reused.

Does it means that those threads before number 3432 not reused? And also consuming the memory?


Updates:

I actually checked the log, for example this thread-255 is completed, but there is no task is scheduled in this thread anymore.

2019-07-08 22:23:37.407  INFO 1 --- [TaskExecutor-255] c.d.v.c.j.impl.LoadSomethingAsync       : Something is running
hades
  • 4,294
  • 9
  • 46
  • 71

2 Answers2

2

Yes, it is unlimited. As @Async depends on SimpleAsyncTaskExecutor by default. And it says in its doc

Supports limiting concurrent threads through the "concurrencyLimit" bean property. By default, the number of concurrent threads is unlimited.

Qingfei Yuan
  • 1,196
  • 1
  • 8
  • 12
  • So I assume that as long as the springboot is not restart, it will consume alot of memory right,? As the thread number goes up.. – hades Jul 08 '19 at 18:04
  • @hades It should depend on the thread usage, if the thread is written in a way that it's long living then new thread keeps spawning. But with the name `TestingService` it sounds like a valid candidate for `@Service` that will should make it `Singleton scope`, just don't use shared variables between methods (side effects) – Vishrant Jul 08 '19 at 18:12
  • This reads different to me: "the number of *concurrent* threads". This doesn't say anything about threads not being reused. I would rather suspect there is a minimal number of threads and they are being released and new ones created. It looks to me like you should set a `corePoolSize` as the default is 1. – Christopher Schneider Jul 08 '19 at 18:13
  • @Christopher ah you made some good point, I searched online but can't really understand the corePoolSize concept, mind to elaborate? – hades Jul 08 '19 at 18:26
  • @hades Take a look at [this question](https://stackoverflow.com/questions/17659510/core-pool-size-vs-maximum-pool-size-in-threadpoolexecutor) – Christopher Schneider Jul 08 '19 at 18:36
  • @ChristopherSchneider from the doc @Qingfei Yuan posted, it stated that `NOTE: This implementation does not reuse threads! Consider a thread-pooling TaskExecutor implementation instead, in particular for executing a large number of short-lived tasks.` – hades Jul 09 '19 at 05:27
1

Does it means that those threads before number 3432 not reused?

Yes. Because are different Process and Threads.

You need to understandt that One Thread 1 Fires... Then Thread 2 Fires. How the application will know how to handle those ID of the Threads?

And also consuming the memory?

No. Of course not it is just setting the ID of the thread is not that all other threads is still running. The life cicle of the other threads ended nothing else.

Gatusko
  • 2,503
  • 1
  • 17
  • 25