zoukankan      html  css  js  c++  java
  • 锁与theadLocal的合并使用

    package cn.com.do1.component.yopin.util;

    import cn.com.do1.common.exception.BaseException;
    import cn.com.do1.common.framebase.dqdp.IBaseDAO;
    import cn.com.do1.component.ordermanage.order.model.Tb91OrderInfoPO;
    import cn.com.do1.component.ordermanage.order.model.Tb91OrderItmPO;
    import cn.com.do1.component.yopin.model.Tb91BeggingPO;

    import java.util.*;
    import java.util.concurrent.TimeUnit;
    import java.util.concurrent.locks.Lock;
    import java.util.concurrent.locks.ReentrantLock;

    /**
    * Created by ao.ouyang on 2015/8/3.
    * //处理抢订单操作
    */
    public class OrderSyncUtil {
    private static OrderSyncUtil orderSyncUtil;
    private final static Lock lock = new ReentrantLock(true);//公平锁
    private final static ThreadLocal<Integer> execCount= new ThreadLocal<Integer>();//执行次数

    public static OrderSyncUtil getInstance() throws Exception{
    execCount.set(5); // 初始化执行次数
    if(null == orderSyncUtil){
    try {
    lock.lock();;
    orderSyncUtil = (cn.com.do1.component.yopin.util.OrderSyncUtil) Class.forName("cn.com.do1.component.yopin.util.OrderSyncUtil").newInstance();
    }finally {
    lock.unlock();
    }
    }
    return orderSyncUtil;
    }

    public Tb91OrderItmPO gradOrder(IBaseDAO baseDAO,Map inputMap) throws Exception,BaseException{
    Tb91OrderItmPO itmPO = null;
    if(lock.tryLock(100, TimeUnit.MILLISECONDS)){
    try {
    itmPO = doGrad(baseDAO, inputMap);
    }finally {
    lock.unlock();
    }
    }else{
    execCount.set(execCount.get()-1);
    if(execCount.get() <= 0){
    throw new Exception("指定时间内未获取到锁,请重试!");
    }else{
    itmPO = gradOrder(baseDAO, inputMap);
    }
    }
    return itmPO;
    }

    public Tb91OrderInfoPO orderPayMent(IBaseDAO baseDAO,Map inputMap) throws Exception,BaseException{
    Tb91OrderInfoPO infoPO = null;
    if(lock.tryLock(100, TimeUnit.MILLISECONDS)){
    try {
    infoPO = doPayMent(baseDAO, inputMap);
    }finally {
    lock.unlock();
    }
    }else{
    execCount.set(execCount.get()-1);
    if(execCount.get() <= 0){
    throw new Exception("指定时间内未获取到锁,请重试!");
    }else{
    infoPO = doPayMent(baseDAO, inputMap);
    }
    }
    return infoPO;
    }

    private Tb91OrderInfoPO doPayMent(IBaseDAO baseDAO,Map inputMap) throws Exception,BaseException{
    String orderCode = inputMap.get("orderCode").toString();
    Integer amount = Integer.valueOf(inputMap.get("amount").toString());
    HashMap paramMap = new HashMap();
    paramMap.put("orderCode", orderCode);
    Tb91OrderInfoPO infoPO =(Tb91OrderInfoPO) baseDAO.searchByField(Tb91OrderInfoPO.class, "select * from tb_91_order_info where ORDER_CODE=:orderCode and ORDER_STATUS in (0,2)", paramMap, true).get(0);
    infoPO.setRemainAmount(infoPO.getRemainAmount()-amount);
    baseDAO.update(infoPO,false);
    Tb91BeggingPO beggingPO = new Tb91BeggingPO();
    beggingPO.setId(UUID.randomUUID().toString());
    beggingPO.setAmount(amount.floatValue());
    beggingPO.setOrderCode(orderCode);
    beggingPO.setGoodsDesc(infoPO.getGoodsDesc());
    beggingPO.setBeggarUsrId(infoPO.getUsrId());
    beggingPO.setCreateTime(new Date());
    beggingPO.setGiverUsrId(inputMap.get("openid").toString());
    beggingPO.setGiverUsrName(inputMap.get("nickname").toString());
    beggingPO.setBeggarUsrName("");
    baseDAO.insertData(beggingPO);
    if(infoPO.getRemainAmount() <=0){
    throw new Exception("订单已经筹满金额了!!!");
    }
    return infoPO;
    }

    private Tb91OrderItmPO doGrad(IBaseDAO baseDAO,Map inputMap)throws Exception,BaseException{
    HashMap paramMap = new HashMap();
    paramMap.put("orderCode",inputMap.get("orderCode"));
    List<Tb91OrderItmPO> itmPOs =baseDAO.searchByField(Tb91OrderItmPO.class, "select * from tb_91_order_itm where ORDER_CODE =:orderCode", paramMap, true);
    if(itmPOs.size()<=0){
    throw new Exception("你来晚了。礼品已经被抢光了!!!。。");
    }
    for(Tb91OrderItmPO itmPO : itmPOs){
    if(inputMap.get("openid").toString().equalsIgnoreCase(itmPO.getReceiverUstId())){
    throw new Exception("你已经获取过礼品了!!!");
    }
    }
    Tb91OrderItmPO itmPO = itmPOs.get(0);
    String openid = inputMap.get("openid").toString();
    String province = inputMap.get("province").toString();
    String city = inputMap.get("city").toString();
    String concatName= inputMap.get("concatName").toString();
    String phone = inputMap.get("phone").toString();
    String addr = inputMap.get("addr").toString();
    itmPO.setProvince(province);
    itmPO.setCity(city);
    itmPO.setShipmentAddr(addr);
    itmPO.setReceiverUstId(openid);
    itmPO.setReceiverUserName(concatName);
    itmPO.setMobilePhone(phone);
    baseDAO.update(itmPO,false);
    return itmPO;
    }
    }
  • 相关阅读:
    Intel x86
    FPGA自计数六位共阳极数码管动态显示2(调用task的方法)
    FPGA六位共阳极数码管动态显示
    运算放大器是模拟电路的基石,模拟电路设计
    这样讲你就懂了!大牛给你介绍《信号与系统》
    电容计算公式
    fork...join的用法
    芯片电源管脚的去耦电容究竟要用多大的?
    Blog Contents
    linux grep 命令常见用法
  • 原文地址:https://www.cnblogs.com/signheart/p/6606484.html
Copyright © 2011-2022 走看看