实现 无线循环 交替 ,比如: 1234 4321 1234 4321 ......
public Integer selectCaseRuleUserTest(int i,String caseRuleName,Map<String,Map<String, LinkedList<Integer>>> map){
Integer caseRuleUser=null;
Map<String, LinkedList<Integer>> caseRuleUserMap=map.get(caseRuleName);
LinkedList<Integer> zhengLinkdList=caseRuleUserMap.get("zheng");
LinkedList<Integer> fanLinkdList=caseRuleUserMap.get("fan");
LinkedList<Integer> zhongLinkdList=caseRuleUserMap.get("zhong");
System.out.println(zhengLinkdList.isEmpty());
if(!zhengLinkdList.isEmpty()){//判断不为空
Integer zhengCaseRuleUser=zhengLinkdList.removeFirst();//取出的同时删除
fanLinkdList.addFirst(zhengCaseRuleUser);
caseRuleUser=zhengCaseRuleUser;
}else{//为空
if(!fanLinkdList.isEmpty()){//不为空
Integer fanCaseRuleUser=fanLinkdList.removeFirst();//取出的同时删除
zhongLinkdList.addFirst(fanCaseRuleUser);
caseRuleUser=fanCaseRuleUser;
}else if(zhengLinkdList.isEmpty()&&fanLinkdList.isEmpty()){ //先取出一个 在
Integer zhongCaseRuleUser=zhongLinkdList.removeFirst();
fanLinkdList.addFirst(zhongCaseRuleUser);
for (Iterator<Integer> dd = zhongLinkdList.iterator(); dd.hasNext();) {
Integer caseRuleUser2 = dd.next();
zhengLinkdList.addLast(caseRuleUser2);
dd.remove();
}
caseRuleUser=zhongCaseRuleUser;
}
}
caseRuleUserMap.put("zheng",zhengLinkdList);
caseRuleUserMap.put("fan",fanLinkdList);
caseRuleUserMap.put("zhong",zhongLinkdList);
map.put(caseRuleName,caseRuleUserMap);
System.out.println("--------------------end-----------"+caseRuleUser+"--------------------");
return caseRuleUser;
}
public static void main(String[] args) {
Map<String,Map<String, LinkedList<Integer>>> map=new HashMap<>();
Map<String, LinkedList<Integer>> map2=new HashMap<>();
LinkedList<Integer> zhengList=new LinkedList<Integer>();
zhengList.add(1);
zhengList.add(2);
zhengList.add(3);
zhengList.add(4);
LinkedList<Integer> fanList=new LinkedList<>();
LinkedList<Integer> zhongList=new LinkedList<>();
map2.put("zheng",zhengList);
map2.put("fan",fanList);
map2.put("zhong",zhongList);
map.put("1",map2);
CaseRuleServiceImpl caseRuleServiceImpl=new CaseRuleServiceImpl();
for (int i=0 ;i<50 ; i++ ){
Integer a=caseRuleServiceImpl.selectCaseRuleUserTest(i,"1",map);
}
}
另外一种解决办法使用redis 的左进 右取
public CaseCollectionGroupUser selectUserByCaseRule(CaseRule caseRule){ RLock lock = redissonClient.getLock("get_token_lock_" + Group_User_Code+"_"+caseRule.getGroupId()); CaseCollectionGroupUser user=null; try { lock.lock(); if(caseRule!=null){ if(redis.opsForList().size(Group_User_Code+"_zhe_"+caseRule.getGroupId())<=0){ List<Object> fanList=redis.opsForList().range(Group_User_Code+"_fan_"+caseRule.getGroupId(),0,-1); redis.opsForList().rightPushAll(RedisConstants.Group_User_Code+"_zhe_"+caseRule.getGroupId(),fanList); redis.delete(RedisConstants.Group_User_Code+"_fan_"+caseRule.getGroupId()); } //123 123 123 123 左边取 右边塞 CaseCollectionGroupUser caseCollectionGroupUser=(CaseCollectionGroupUser)redis.opsForList().leftPop(Group_User_Code+"_zhe_"+caseRule.getGroupId()); redis.opsForList().leftPush(RedisConstants.Group_User_Code+"_fan_"+caseRule.getGroupId(), caseCollectionGroupUser); return caseCollectionGroupUser; } }catch (Exception e){ e.printStackTrace(); log.error("redis异常["+e.getMessage()+"]"); }finally { if (lock != null && lock.isHeldByCurrentThread()) { lock.unlock(); } } return user; }