我们平常如果想使用spring自带的线程池,可以使用@EnableAsync,然后在需要异步调用的方法上加上@Async即可异步调用。但是spring调用异步方法的默认的线程池
SimpleAsyncTaskExecutor却并不是真正意义上的线程池,它会为每一个任务都创建一个线程,这样当我们一次性有很多的任务来时,就会创建大量的线程,可能造成OOM.
/**
* {@link TaskExecutor} implementation that fires up a new Thread for each task,
* executing it asynchronously.
*
* <p>Supports limiting concurrent threads through the "concurrencyLimit"
* bean property. By default, the number of concurrent threads is unlimited.
*
* <p><b>NOTE: This implementation does not reuse threads!</b> Consider a
* thread-pooling TaskExecutor implementation instead, in particular for
* executing a large number of short-lived tasks.
*
* @author Juergen Hoeller
* @since 2.0
* @see #setConcurrencyLimit
* @see SyncTaskExecutor
* @see org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
* @see org.springframework.scheduling.commonj.WorkManagerTaskExecutor
*/
版权声明:本文为weixin_41751625原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。