fis*_*tte 2 java spring asynchronous threadpool threadpoolexecutor
我们在基于Spring的Web应用程序中有一个服务实现,它增加了db中的一些统计计数器.由于我们不想弄乱用户的响应时间,因此我们使用Spring的@Async异步定义它们:
public interface ReportingService {
@Async
Future<Void> incrementLoginCounter(Long userid);
@Async
Future<Void> incrementReadCounter(Long userid, Long productId);
}
Run Code Online (Sandbox Code Playgroud)
和spring任务配置如下:
<task:annotation-driven executor="taskExecutor" />
<task:executor id="taskExecutor" pool-size="10" />
Run Code Online (Sandbox Code Playgroud)
现在,有pool-size="10"两个线程尝试两个线程时,我们会遇到并发问题,创建包含计数器的相同初始记录.
这里设置pool-size="1"避免这些冲突是一个好主意吗?这有副作用吗?我们有很多地方可以触发异步操作来更新统计信息.
| 归档时间: |
|
| 查看次数: |
5659 次 |
| 最近记录: |