| | |
| | | package com.supersavedriving.user.modular.system.util; |
| | | |
| | | import com.supersavedriving.user.core.util.ToolUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.RedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.StringUtils; |
| | | import redis.clients.jedis.Jedis; |
| | | import redis.clients.jedis.JedisPool; |
| | | import redis.clients.jedis.Pipeline; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | import java.util.UUID; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | |
| | | int num1 = 1; |
| | | while (num1 <= 10){ |
| | | try { |
| | | Thread.sleep(3000);//等待3秒 |
| | | Thread.sleep(1000);//等待3秒 |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | |
| | | } |
| | | |
| | | |
| | | public boolean lock(String key){ |
| | | try { |
| | | boolean b = lock(key,5); |
| | | if(!b){ |
| | | int num1 = 1; |
| | | while (num1 <= 10){ |
| | | try { |
| | | Thread.sleep(1000);//等待1秒 |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | b = lock(5); |
| | | if(b){ |
| | | return true; |
| | | }else{ |
| | | num1++; |
| | | } |
| | | } |
| | | return false; |
| | | } |
| | | return b; |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | unlock(); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取redis锁 |
| | | * @param time |
| | | * @return |
| | | */ |
| | | public boolean lock(String key, int time){ |
| | | String uuid = UUID.randomUUID().toString(); |
| | | return lock(key, uuid, time); |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 获取redis锁 |
| | | * @param time |