最近碰到一个这样的功能要求:怎么在一个类里面,实现高效并发处理下只可以初始化一次的方法?

实现方式:

1)volatile方式:

  1. /**
  2. * Created by Chengrui on 2015/7/28.
  3. */
  4. public class MyThread {
  5. private static volatile boolean initialized = false;
  6. public void init(){
  7. if(initialized == false){
  8. initialized = true;
  9. //here is the initialization code
  10. }
  11. }
  12. }

2)AtomicBoolean方式:

  1. /**
  2. * Created by Chengrui on 2015/7/28.
  3. */
  4. public class MyThread {
  5. private static AtomicBoolean initialized = new AtomicBoolean(false);
  6. public void init(){
  7. if(initialized.compareAndSet(false, true)){
  8. //here is the initialization code
  9. }
  10. }
  11. }

附:AtomicBoolean帮助文档:

最新文章

  1. Window.focus()让页面成为当前窗体
  2. 使用未付费的账号真机调试 iOS 程序,过几天后程序一打开就会闪退
  3. 批处理命令——call 和 start
  4. WebForm增删改查
  5. MVC OF UEditor 图片上传- 额外参数 笔记
  6. quartz 数据表字典
  7. 推荐一款开源的原型设计软件--pencil
  8. Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp
  9. hadoop命令报错:权限问题
  10. JS闭包,以及适用场景
  11. 多重外键关系在java中的处理方案
  12. Linux Shell 命令--grep
  13. 敏捷开发的道与术---MPD软件工作坊培训感想(上)
  14. Laravel 多where组合
  15. Raft 一致性协议算法 《In search of an Understandable Consensus Algorithm (Extended Version)》
  16. 【BZOJ1226】学校食堂(动态规划,状态压缩)
  17. sklearn的train_test_split
  18. NO2——最短路径
  19. hbase shell删除键不听使唤
  20. 分布式计算中WebService的替代方案: RPC (XML-RPC | JSON-RPC)

热门文章

  1. Python - 字典(dict)删除元素
  2. CList 点击表头排序 (2)两种排序方法中其中一种
  3. 【Codeforces Round #452 (Div. 2) B】Months and Years
  4. 洛谷 P2067 Cytus-Holyknight
  5. 邮件协议与port
  6. HDU1203 I NEED A OFFER! 【贪心】
  7. 如何让Apache不显示服务器信息
  8. chrome 的input 上传响应慢问题解决方案
  9. CView::OnPreparePrinting
  10. 洛谷 P2095 营养膳食