Java線程安全計數器的簡單程式碼範例
這篇文章主要介紹了Java線程安全的計數器簡單實作程式碼範例,具有一定參考價值,需要的朋友可以了解下。
前幾天工作中一段業務代碼需要一個變數每天從1開始遞增。為此自己簡單的封裝了一個線程安全的計數器,可以讓一個變數每天從1開始遞增。當然了,如果項目在運作中發生重啟,即便日期還是當天,還是會從1開始重新計數。所以把計數器的值儲存在資料庫中會更可靠,不過這不影響這段程式碼的價值,現在貼出來,供有需要的人參考。
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線程安全計數器的簡單程式碼範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Java 8引入了Stream API,提供了一種強大且表達力豐富的處理數據集合的方式。然而,使用Stream時,一個常見問題是:如何從forEach操作中中斷或返回? 傳統循環允許提前中斷或返回,但Stream的forEach方法並不直接支持這種方式。本文將解釋原因,並探討在Stream處理系統中實現提前終止的替代方法。 延伸閱讀: Java Stream API改進 理解Stream forEach forEach方法是一個終端操作,它對Stream中的每個元素執行一個操作。它的設計意圖是處

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。

PHP適用於Web開發和內容管理系統,Python適合數據科學、機器學習和自動化腳本。 1.PHP在構建快速、可擴展的網站和應用程序方面表現出色,常用於WordPress等CMS。 2.Python在數據科學和機器學習領域表現卓越,擁有豐富的庫如NumPy和TensorFlow。
