使用redis简单模拟抢购

使用redis简单模拟抢购

使用inc自增

/***
 * 测试redis inc并发
 *
 */
public class RedisThreadInc {
	private static AtomicInteger SUCCESS = new AtomicInteger(1); //原子操作类
	private static Random random = new Random();//生成随机结果
	
	
	public static void main(String[] args) {
		String redisKey = "product_inc";
		
		Jedis jdeJedis = new Jedis("192.168.81.132",6379);
		jdeJedis.auth("password");
		String set = jdeJedis.set(redisKey,"1");
		jdeJedis.close();
		ExecutorService fiExecutorService = Executors.newFixedThreadPool(10); //固定线程池 
		
		for (int i = 0; i < 100000 ; i++) {
			fiExecutorService.submit(()->{
				//定义执行任务的内容
				Jedis jRedis = new Jedis("192.168.81.132",6379);
				jRedis.auth("password");
				String string = jRedis.get(redisKey);
				
				try {
					if(Integer.valueOf(string) < 100 ) {
						//模拟其他业务执行时间
						Thread.sleep(300); //300毫秒
						Long incr = jRedis.incr(redisKey);
						if( incr <= 100 ) {
							if(random.nextBoolean()) {
								int count = SUCCESS.incrementAndGet(); //记录成功数
								System.out.println("success:"+count);
							}else {
								System.out.println("因其他原因失败,将商品加回来");
								jRedis.decr(redisKey);
							}
						}else {
							System.out.println("已经为空了");
							Thread.sleep(1000);
							jRedis.decr(redisKey);
						}
					}else {
						System.out.println("已经为空了");
						Thread.sleep(1000);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				jRedis.close();
			});
		}
		
		fiExecutorService.shutdown();
		
	}
}

使用队列

/***
 * 测试redis 队列 并发
 *
 */
public class RedisThreadList {
	private static AtomicInteger SUCCESS = new AtomicInteger(1); //原子操作类
	private static Random random = new Random(); //生成随机结果
	
	
	public static void main(String[] args) {
		String redisKey = "product_list";
		
		Jedis jdeJedis = new Jedis("192.168.81.132",6379);
		jdeJedis.auth("password");
		
		for (int i = 1; i < 100; i++) { //存入100个商品
			jdeJedis.rpush(redisKey, "1");
		}
		jdeJedis.close();
		
		ExecutorService fiExecutorService = Executors.newFixedThreadPool(10);  //固定线程池 
		
		for (int i = 0; i < 100000 ; i++) {
			fiExecutorService.submit(()->{
				//定义执行任务的内容
				Jedis jRedis = new Jedis("192.168.81.132",6379);
				jRedis.auth("password");
				Long llen = jRedis.llen(redisKey);
				boolean flag = true;
				try {
					if(llen > 0 ) {
						flag = false;
						//其他业务执行时间
						Thread.sleep(300); //300毫秒
						String key = jRedis.lpop(redisKey);
						if(key != null) {
							if(random.nextBoolean()) {
								int count = SUCCESS.incrementAndGet(); //记录成功数
								System.out.println("success:"+count);
							}else {
								System.out.println("因其他原因失败,将商品入队列");
								jRedis.rpush(redisKey,"1");
							}
						}else {
							flag = true;
						}
					}
					
					if(flag) {
						System.out.println("已经为空了");
						Thread.sleep(1000);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				jRedis.close();
			});
		}
		
		fiExecutorService.shutdown();
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值