同步synchronized用法
2016-01-19 22:03
459 查看
今天在高人的指导下,对同步synchronized用法有了更高一层的理解,非常感谢他的无私奉献。在此把代码贴出来方便日后查阅。
publicclass SfServlet { privatestatic ExpressInfoService expressInfoService=null; privatestatic Object lock =new Object(); privatestatic ExpressInfoService getExpressInfoService() { if (expressInfoService ==null) { //1、用lock把锁的范围缩小,提高效率。 //2、synchronized (SfServlet.class): 把整个SfServlet类锁住,这样很糟糕并且效率低下。比如调用其他方法,也要等得到锁才能继续做。 synchronized (lock) { if (expressInfoService ==null) expressInfoService =new ExpressInfoServiceBean(); } } return expressInfoService; } }
相关文章推荐
- 使用scipy.spatial.Delaunay 三角网的构建
- java数组内存分析
- python核心编程-使用线程锁
- mysql在centos下的安装
- CRLF injection 简单总结
- Python 5.4 定制类
- 机器学习导论
- VirtualBox安装时发生严重错误的解决办法
- 【LeetCode OJ 263】Ugly Number
- kingbaseES表存在,但是select查询不到,提示表不存在处理方式
- ExtJS stores - load JSON only once
- leetcode--Pow(x,n)
- Android进阶笔记:Messenger源码详解
- 2015-2016 ACM ICPC Baltic Selection Contest E. Permutation Polygon
- Java异常时的返回值
- 富文本编辑器——kindeditor+七牛
- Java堆和栈
- 【环境配置】配置sdk
- paper 10:支持向量机系列七:Kernel II —— 核方法的一些理论补充,关于 Reproducing Kernel Hilbert Space 和 Representer Theorem 的简介。
- C语言 -- 折半查找小程序