10000个线程更新同一行数据

 
  
@Test
public void threadTestOrderBy() throws InterruptedException {

final Long id=141284830240768L;
int threadCount=1000;
final CountDownLatch begin = new CountDownLatch(1);
final CountDownLatch end = new CountDownLatch(threadCount);
final int[] result={0,0};
final Object lock = new Object();
ExecutorService executorService = Executors.newFixedThreadPool(1000);

for (int i = 0; i < threadCount; i++) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
begin.await();
baseSiteService.updateOrderBy(id);
synchronized(lock){
result[0]++;
}
} catch (Exception ex) {
synchronized(lock) {
result[1]++;
}
ex.printStackTrace();
}finally{
end.countDown();
}
}
});
executorService.submit(thread);
}
System.out.println("1000个线程开始");
begin.countDown();
end.await();
System.out.println("1000个线程更新结束");
System.out.println("成功"+result[0]+",失败"+result[1]);
BaseSiteEntity byId = this.baseSiteService.getById(id);
System.out.println(byId.getOrderBy());
}
 
 
  
@Override
@Transactional
public void updateOrderBy(Long id) {
BaseSiteEntity byId = this.baseSiteDao.getById(id);
if (byId.getOrderBy() >= 1) {
this.baseSiteDao.updateOrderBy(id);
} else {
throw new BusinessException("没有库存");
}
}
 
 
<update id="updateOrderBy">
        update t_base_site t set t.order_by=t.order_by-1 where t.site_id=#{id}
    </update>
</mapper>

 

update t_base_site t set t.order_by=1 where t.site_id='141284830240768';

  条件后期不成立,但是已经开始排队更新,导致结果为-数

转载于:https://www.cnblogs.com/zfzf1/p/7768211.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值