天天看點

java背景使用計數器實作輪詢

現在遇到一個業務場景:有四個區域的若幹使用者。每天通過APP上傳照片。四個區域每個區域各配置設定一個評分員對使用者上傳的照片進行評分,評分是匿名的,但為了確定公平公正,每個區域評分員又不能隻對自己本區域的人員照片進行評分。同時每個評分員的評分工作量又要求大緻相同。

這個場景下需要在使用者評分時就指定相應的評分員,但這個指定是随機的,而每個評分員被指定的機率又是相同的。那麼就可以考慮輪詢。我這個場景是廣義上的輪詢。就是說確定每個人按順序被指定配置設定。

最後用java計數器實作了預期功能。首先這個場景是并發的,要求計數器必須是線程間可見的而且是線程安全的。我的實作是這樣的:先建立計數器工具類,確定每個線程取到時加1,且不被覆寫,然後将評分員清單查詢出來,加入hashMap。再在圖檔上傳接口裡取到這個list,利用計數器目前的值對list的長度取餘作為下标去從list裡取評分員對象。因為list有序集合,隻要計數器正常就可以按順序不斷取到評分員對象,實作輪詢。

第一步》建立計數器工具類:

package com.xcwlkj.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

public class CounterUtil {

	    private static final AtomicReferenceFieldUpdater<CounterUtil, AtomicInteger> valueUpdater =
	            AtomicReferenceFieldUpdater.newUpdater(CounterUtil.class, AtomicInteger.class, "value");
	    //保證變量線程間可見
	    private volatile String key;
	    //保證計數器原子性
	    private volatile AtomicInteger value;
	    private static final String DATE_PATTERN = "yyyy-MM-dd";


	    public CounterUtil() {
	        /**
	         *key值設定為目前日期是因為計數器每天重置一次,否則數值一天天增大
	         */
	        this.key = getCurrentDateString() ;
	        this.value = new AtomicInteger(0);
	    }


	    /**
	     * 擷取計數器加1以後的值
	     *
	     * @return
	     */
	    public Integer addAndGet() {

	        AtomicInteger oldValue = value;
	        AtomicInteger newInteger = new AtomicInteger(0);

	        int newVal = -1;
	        String newDateStr = getCurrentDateString();
	        //日期一緻,計數器加1後傳回
	        if (isDateEquals(newDateStr)) {
	            newVal = add(1);
	            return newVal;
	        }

	        //日期不一緻,保證有一個線程重置技術器
	        reSet(oldValue, newInteger, newDateStr);
	        this.key = newDateStr;
	        //重置後加1傳回
	        newVal = add(1);
	        return newVal;
	    }

	    /**
	     * 擷取計數器的目前值
	     * @return
	     */
	    public Integer get() {
	        return value.get();
	    }

	    /**
	     * 判斷目前日期與老的日期(也即key成員變量記錄的值)是否一緻
	     *
	     * @return
	     */
	    private boolean isDateEquals(String newDateStr) {
	        String oldDateStr = key;
	        if (!isBlank(oldDateStr) && oldDateStr.equals(newDateStr)) {
	            return true;
	        }

	        return false;
	    }


	    /**
	     * 如果日期發生變化,重置計數器,也即将key設定為目前日期,并将value重置為0,重置後才能接着累加,
	     */
	    private void reSet(AtomicInteger oldValue, AtomicInteger newValue, String newDateStr) {
	        if(valueUpdater.compareAndSet(this, oldValue, newValue)) {
	            System.out.println("線程" + Thread.currentThread().getName() + "發現日期發生變化");
	        }
	    }

	    /**
	     * 擷取目前日期字元串
	     *
	     * @return
	     */
	    private String getCurrentDateString() {
	        Date date = new Date();
	        String newDateStr = new SimpleDateFormat(DATE_PATTERN).format(date);
	        return newDateStr;
	    }

	    /**
	     * 計數器的值加1
	     * @param increment
	     */
	    private int add(int increment) {
	       return value.addAndGet(increment);
	    }

	    public static boolean isBlank(CharSequence cs) {
	        int strLen;
	        if(cs != null && (strLen = cs.length()) != 0) {
	            for(int i = 0; i < strLen; ++i) {
	                if(!Character.isWhitespace(cs.charAt(i))) {
	                    return false;
	                }
	            }

	            return true;
	        } else {
	            return true;
	        }
	    }
}

           

第二步》将待輪詢的評分員放入緩存(代碼片段)

@Service("manageUserService")
public class ManageUserServiceImpl implements ManageUserService {
 private static  Map<String,List<ManageUser>> userMap = new HashMap<String,List<ManageUser>>();
@Override
public void initVillageVerifyRoleCache() {

         ManageUserExample ex=new ManageUserExample();
         ex.createCriteria().andRoleNbrEqualTo(systemCfg.getVillageVerifyRoleNbr());
         //查詢所有評分員清單
       	 List<ManageUser> userList=manageUserMapper.selectByExample(ex);
       	 if (userList.size()>0) {
       	    //清除map所有key-value
       		userMap.clear();
       		//将查詢到的最新值加入map
       		userMap.put(systemCfg.getVerifyKey(), userList);          
        }
  }
  //在修改評分員時重新整理緩存
@Override
public void modifyManageUser(ManageUser model) {
    if (StringUtil.equals(systemCfg.getVillageVerifyRoleNbr(), model.getRoleNbr())) {
         initVillageVerifyRoleCache();
    }
  }
  //從緩存擷取評分員清單
@Override
public List<ManageUser> queryManageUsersFromCache(String veriftKey) {
	
	return userMap.get(veriftKey);
}
}
           

第三步》在圖檔上傳的接口裡使用計數器(代碼片段)

@Service("garbageScoreManager")
public class GarbageScoreManagerImpl implements GarbageScoreManager {
    private CounterUtil count=new CounterUtil();
    @Override
	public ReviewScoreResModel reviewScore(ReviewScoreReqModel req) {
		//從緩存取出評分員清單
		List<ManageUser> userList=manageUserService.queryManageUsersFromCache(systemCfg.getVerifyKey());
		//擷取計數器目前值
		int currCount=count.addAndGet();
		if (userList.size()<=0) {
			throw new BusinessParamException("請先配置評分員", "請先配置評分員");
		}
		//取餘算法確定每次都能取到不同評分員對象
		int newCount=currCount%userList.size();
		//擷取評分員對象編号
		String manageUserNbr=userList.get(newCount).getNbr();
	}
}


    
           

第四步》按評分員編号查詢照片記錄(實作略)

第五步》将hashmap在啟動時放置在spring上下文裡

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;
//初始化啟動類
@Service("systemInitService")
public class SystemInitService implements ApplicationListener<ContextRefreshedEvent> {
	@Autowired
	private OrganService organService;
	@Autowired
	private ManageUserService manageUserService;
	private Logger log = LoggerFactory.getLogger(SystemInitService.class);
	/** 
	 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		
		if (event.getApplicationContext().getParent() == null) {

			log.info("-----------初始化評分員到緩存開始--------------");
			manageUserService.initVillageVerifyRoleCache();
			log.info("-----------初始化評分員到緩存結束--------------");
	    }
		
	}

}
`
           

繼續閱讀