Java订单号生成工具(实现二)基于队列
来源:互联网 发布:avmoo最新的域名 编辑:程序博客网 时间:2024/05/26 22:10
使用了ConcurrentLinkedQueue,ConcurrentLinkedQueue是一个基于链接节点的无界线程安全队列,它采用先进先出的规则对节点进行排序,当我们添加一个元素的时候,它会添加到队列的尾部,当我们获取一个元素时,它会返回队列头部的元素。它采用了“wait-free”算法来实现,该算法在Michael & Scott算法上进行了一些修改, Michael & Scott算法的详细信息可以参见参考资料一。
关于ConcurrentLinkedQueue详情:http://ifeve.com/concurrentlinkedqueue/
package util;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.CountDownLatch;import java.util.concurrent.TimeUnit;import play.Configuration;public class OrderNumberGenerator { private static final ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>(); private static final CountDownLatch latch = new CountDownLatch(1); /** * 每毫秒生成订单号数量最大值,约定取整百,整千。 */ public static final int maxPerMSECSize = Configuration.root().getInt("maxPerMSECSize", 1000); private static void init() { for (int i = 0; i < maxPerMSECSize; i++) { queue.offer(i); } latch.countDown(); } public static Integer poll() { try { if (latch.getCount() > 0) { init(); latch.await(1, TimeUnit.SECONDS); } } catch (InterruptedException e) { e.printStackTrace(); } Integer i = queue.poll(); queue.offer(i); return i; } public static String get() { long nowLong = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())); String number = maxPerMSECSize + poll() + ""; return nowLong + number.substring(1); }}
0 0
- Java订单号生成工具(实现二)基于队列
- Java订单号生成工具(实现二)基于队列
- Java订单号生成工具(实现二)基于队列
- Java订单号生成工具(实现一)基于对象锁
- Java订单号生成工具(实现一)基于对象锁
- Java订单号生成策略(基于消息队列)
- java 生成订单号
- Java 生成订单号
- java生成唯一订单号
- JAVA生成有规则的订单号
- java生成订单号的一种思路(生成友好的订单号)
- java如何根本生成10位号(比如订单号)
- java工具使用-01-使用commons.lang3生成随机的20位的订单号
- 生成订单号
- 生成订单号
- 订单号生成
- 订单号生成
- 生成订单号
- 【LeetCode】Minimum Depth of Binary Tree 解题报告
- instantclient-basic-nt-11.2.0.3.0配置 PLSQL Developer远程连接
- Hadoop —— Ubuntu单机环境部署Hadoop1.2.1
- Windows下Nginx的启动、停止等命令
- 机器学习入门:线性回归及梯度下降
- Java订单号生成工具(实现二)基于队列
- java操作cookie 实现两周内自动登录 加密安全性问题
- Bash Shell 重定向(一):原文
- conversion to dalvik format failed with error 1。
- spring的下载地址
- 软件测试,想说爱你不容易
- Ubuntu14配置laravel5开发环境
- Spring Bean获取applicationcontext方法
- Linux shell脚本执行提示错误[[: not found