ThreadLocal使用
2015-05-30 16:43
381 查看
首先直接看如何使用ThreadLocal
主要三个步骤,1,你要处理那个类
2,是否在一个ThreadLocal里,没有的话,就新建一个,如果有的话,就从ThreadLocal里取出来
3,最后在ThreadLocal的处理类上进行操作。
感觉思想真的很简单,但是有一个问题,如果处理的过程很多,同一个处理线程里,多个线程(不可能的啦),是不是会奔溃掉。
public class TaobaoPurchaseTaskNettyHandler { private static final String END_WORKING_TIME = "230000"; private static Logger logger = LoggerFactory.getLogger(TaobaoPurchaseTaskNettyHandler.class); private static ThreadLocal<TaobaoPurchaseOperatorNetty> operatorThreadLocal = new ThreadLocal<>(); public static CheckPricePlaceOrderAndPayResponse placeOrderAndPay(CheckPricePlaceOrderAndPayRequest placeOrderAndPayRequest)throws Exception{ // if(!checkPriceOrder.getOldOrderId().equals("75579381339")) // return; // if(!task.getTaobaoOrderId().equals("75167292951")) // return; MDC.put(CrawlerConstants.MDC_TRACEID_PLACEHOLDER, RandomStringUtils.randomAlphanumeric(10)); //TaobaoPurchaseOperation.isOrderNeedTaobaoPurchase(task)&& if ( DateTimeUtil.time6().compareTo(END_WORKING_TIME) <= 0){ logger.info(new Gson().toJson(placeOrderAndPayRequest)); TaobaoPurchaseOperatorNetty operator = getOperator(); return operator.OperateTaobaoPurchase(placeOrderAndPayRequest); }else{ return null; } } private static TaobaoPurchaseOperatorNetty getOperator(){ TaobaoPurchaseOperatorNetty operator = operatorThreadLocal.get(); if(operator == null){ operator = new TaobaoPurchaseOperatorNetty(); operatorThreadLocal.set(operator); } return operator; } }
主要三个步骤,1,你要处理那个类
2,是否在一个ThreadLocal里,没有的话,就新建一个,如果有的话,就从ThreadLocal里取出来
3,最后在ThreadLocal的处理类上进行操作。
感觉思想真的很简单,但是有一个问题,如果处理的过程很多,同一个处理线程里,多个线程(不可能的啦),是不是会奔溃掉。
相关文章推荐
- Windows、Ubuntu双系统下,给Ubuntu增加磁盘空间
- 数据库连接池的配置(mysql+sql2000+sql2008+sybaase)
- js加载顺序测试
- 电脑实用小技巧
- 禁止uiscrollview垂直方向滚动,只允许水平方向滚动;或只允许垂直方向滚动
- HTML5的localStorage、sessionStorage和SQL的CRUD的使用
- Serializable接口
- Java使用Comparable解决排序问题
- 求解逆元
- 五款 免费WAMP服务器
- 使用 shared_ptr
- Asp.Net北大青鸟总结(三)-应用程序状态管理
- c++:不要重新定义继承而来的non-virtual函数
- 经典面试题集锦
- 影响SQL Server数据库应用性能的几个常见因素
- mysql数据库错误解决方法
- STL--vector
- 基于net.tcp的WCF配置实例解析
- STL容器之串行容器
- 考场有感