Java线程安全计数器的简单代码示例
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中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
1 个月前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保护它?
3 周前
By DDD

热工具

记事本++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中的每个元素执行一个操作。它的设计意图是处
