一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java線程安全的計數器簡單實現代碼示例

Java線程安全的計數器簡單實現代碼示例

2021-01-17 14:20lhever_ Java教程

這篇文章主要介紹了Java線程安全的計數器簡單實現代碼示例,具有一定參考價值,需要的朋友可以了解下。

前幾天工作中一段業務代碼需要一個變量每天從1開始遞增。為此自己簡單的封裝了一個線程安全計數器,可以讓一個變量每天從1開始遞增。當然了,如果項目在運行中發生重啟,即便日期還是當天,還是會從1開始重新計數。所以把計數器的值存儲在數據庫中會更靠譜,不過這不影響這段代碼的價值,現在貼出來,供有需要的人參考。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.hikvision.cms.rvs.common.util;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
/**
 * Created by lihong10 on 2017/8/9.
 * 一個循環計數器,每天從1開始計數,隔天重置為1。
 * 可以創建一個該類的全局對象,然后每次使用時候調用其get方法即可,可以保證線程安全性
 */
public class CircularCounter {
  private static final AtomicReferenceFieldUpdater<CircularCounter, AtomicInteger> valueUpdater =
      AtomicReferenceFieldUpdater.newUpdater(CircularCounter.class, AtomicInteger.class, "value");
  //保證內存可見性
  private volatile String key;
  //保證內存可見性
  private volatile AtomicInteger value;
  private static final String DATE_PATTERN = "yyyy-MM-dd";
  public CircularCounter() {
    /**
     * 這里將key設置為getCurrentDateString() + "sssssssssss" 是為了測試addAndGet()方法中日期發生變化的情況
     * 正常使用應該將key初始化為getCurrentDateString()
     */
    this.key = getCurrentDateString() + "sssssssssss";
    this.value = new AtomicInteger(0);
  }
  /**
   * 獲取計數器加1以后的值
   *
   * @return
   */
  public Integer addAndGet() {
    AtomicInteger oldValue = value;
    AtomicInteger newInteger = new AtomicInteger(0);
    int newVal = -1;
    String newDateStr = getCurrentDateString();
    //日期一致,計數器加1后返回
    if (isDateEquals(newDateStr)) {
      newVal = add(1);
      return newVal;
    }
    //日期不一致,保證有一個線程重置技術器
    reSet(oldValue, newInteger, newDateStr);
    this.key = newDateStr;
    //重置后加1返回
    newVal = add(1);
    return newVal;
  }
  /**
   * 獲取計數器的當前值
   * @return
   */
  public Integer get() {
    return value.get();
  }
  /**
   * 判斷當前日期與老的日期(也即key成員變量記錄的值)是否一致
   *
   * @return
   */
  private boolean isDateEquals(String newDateStr) {
    String oldDateStr = key;
    if (!isBlank(oldDateStr) && oldDateStr.equals(newDateStr)) {
      return true;
    }
    return false;
  }
  /**
   * 如果日期發生變化,重置計數器,也即將key設置為當前日期,并將value重置為0,重置后才能接著累加,
   */
  private void reSet(AtomicInteger oldValue, AtomicInteger newValue, String newDateStr) {
    if(valueUpdater.compareAndSet(this, oldValue, newValue)) {
      System.out.println("線程" + Thread.currentThread().getName() + "發現日期發生變化");
    }
  }
  /**
   * 獲取當前日期字符串
   *
   * @return
   */
  private String getCurrentDateString() {
    Date date = new Date();
    String newDateStr = new SimpleDateFormat(DATE_PATTERN).format(date);
    return newDateStr;
  }
  /**
   * 計數器的值加1。采用CAS保證線程安全性
   *
   * @param increment
   */
  private int add(int increment) {
    return value.addAndGet(increment);
  }
  public static boolean isBlank(CharSequence cs) {
    int strLen;
    if(cs != null && (strLen = cs.length()) != 0) {
      for(int i = 0; i < strLen; ++i) {
        if(!Character.isWhitespace(cs.charAt(i))) {
          return false;
        }
      }
      return true;
    } else {
      return true;
    }
  }
  public static void test() {
    CircularCounter c = new CircularCounter();
    AtomicInteger count = new AtomicInteger(0);
    List<Thread> li = new ArrayList<Thread>();
    int size = 10;
    CountDownLatch latch1 = new CountDownLatch(1);
    CountDownLatch latch2 = new CountDownLatch(size);
    for (int i = 0; i < size; i++) {
      Thread t = new Thread(new CounterRunner(c, latch1, latch2, count), "thread-" + i);
      li.add(t);
      t.start();
    }
    System.out.println("start");
    latch1.countDown();
    try {
      latch2.await();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println(count.get());
    System.out.println(c.get());
    if(count.get() == c.get()) {
      System.out.println("該計數器是線程安全的!!!");
    }
  }
  public static void main(String... args) {
    for(int i = 0; i < 15; i++) {
      test();
    }
  }
}
/**
 * 測試使用的Runnable對象
 */
class CounterRunner implements Runnable {
  private CircularCounter counter;
  private CountDownLatch latch1;
  private CountDownLatch latch2;
  private AtomicInteger count;
  public CounterRunner(CircularCounter counter, CountDownLatch latch1, CountDownLatch latch2, AtomicInteger count) {
    this.latch1 = latch1;
    this.latch2 = latch2;
    this.counter = counter;
    this.count = count;
  }
  @Override
  public void run() {
    try {
      latch1.await();
      System.out.println("****************");
      for (int i = 0; i < 20; i++) {
        counter.addAndGet();
        count.addAndGet(1);
      }
      latch2.countDown();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

總結

以上就是本文關于Java線程安全的計數器簡單實現代碼示例的內容,希望對大家有所幫助,有什么問題可以隨時留言,歡迎大家一起交流討論。感謝朋友們對服務器之家網站的支持!

原文鏈接:http://blog.csdn.net/nmgrd/article/details/77015206

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩日韩日韩手机看片自拍 | 美女天天色 | 日本中文字幕二区三区 | 456成人免费高清视频 | 久久88综合| 成成人看片在线 | yy6080欧美三级理论 | 国产免费成人在线视频 | 我半夜摸妺妺的奶C了她 | kk4444了欧美| 久久精品亚洲热综合一本 | 欧美2区 | 色综合久久天天综合 | 亚州综合网 | 青青国产成人久久激情911 | 婷婷综合亚洲 | 99精品视频在线观看re | 国产精品99久久久 | 国产成人亚洲精品91专区手机 | 9420高清视频在线观看网百度 | 欧美激情亚洲 | 国内精品在线播放 | 色视频久久 | 美女被绑着吸下部的故事 | 无码天堂亚洲国产AV久久 | 免费在线看a | 精品午夜久久福利大片免费 | 久久精品嫩草影院免费看 | 久久99精国产一区二区三区四区 | 欧美另类xxx精品人妖 | 免费国产之a视频 | 奇米影视888四色首页 | 国产成人夜色影视视频 | 日韩欧美国产免费看清风阁 | 亚洲国产精品ⅴa在线观看 亚洲国产高清一区二区三区 | 性xxx免费视频 | 好大用力深一点女公交车 | 男生和女生搞逼逼 | 亚洲 欧美 国产 视频二区 | 91视频免费观看网站 | 好看的亚洲视频 |