阻塞队列 ArrayBlockingQueue 我给自己挖了一个坑

说一句MMB,

一下午时间,

package cn.tbnb1.seckil.quene;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Callable;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpServletRequest;















import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.tbnb1.common.utils.CommonConstant;
import cn.tbnb1.model.Goods;
import cn.tbnb1.service.redis.ICacheService;
import cn.tbnb1.utils.SpringContextUtil;


/**
 * 执行请求的工作线程
 * @author Administrator
 *
 */

public class RequestProcessorThread implements Callable<Boolean>  {
    
    
    

    /**
     * 自己监控的内存队列
     */
    private ArrayBlockingQueue<HttpServletRequest> queue;
    public RequestProcessorThread(ArrayBlockingQueue<HttpServletRequest> queue) {
        this.queue = queue;
        
    }
    
    @Override
    public Boolean call() throws Exception {
        try {
            while(true) {
                // ArrayBlockingQueue
                // Blocking就是说明,如果队列满了,或者是空的,那么都会在执行操作的时候,阻塞住
                HttpServletRequest request = queue.take();
            synchronized (this) {
                ICacheService redisCacheService=(ICacheService)SpringContextUtil.getBean("redisCacheService");
                String itemId=    request.getParameter("id");//商品id
            Long res=    redisCacheService.get(CommonConstant.ITEMS_STORE+7, Long.class);
                if(res<=0L){//结束秒杀
                        redisCacheService.set(CommonConstant.QUEUE_SISZE,0L,10000);
//                        queue.clear();
//                        return true; 这句话  给自己挖的坑  ,首先我自己是这样想的 ,结束秒杀不就 return 掉,mmb  这样好了,call直接停止了,导致请求的线程全部hodl在这里了,

}else{ Long subtract = redisCacheService.subtract(CommonConstant.ITEMS_STORE+7);//库存 Goods goods = redisCacheService.get("7", Goods.class); String uid = (String)request.getAttribute("Sessionid"); System.out.println("===========抢购结果===========: "+"小菜菜抢购"+goods.toString()); redisCacheService.subtract(CommonConstant.QUEUE_SISZE); } } } } catch (Exception e) { e.printStackTrace(); } return true; } }

 

posted @ 2017-11-30 17:15  指针怒草内存栈  阅读(1951)  评论(0编辑  收藏  举报