代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
同步id生成器
作者:
/ 发布于
2019/3/28
/
1051
同步id生成器
import java.sql.Connection; import org.apache.log4j.Logger; /** * @author rq * */ public class CardIdManager { public static Logger logger = Logger.getLogger(CardIdManager.class); private static CardIdManager cardNum; private Object obj = new Object(); public static CardIdManager getInstance() { if (cardNum == null) { cardNum = new CardIdManager(); } return cardNum; } /** * 同步的方法块获取唯一的商城卡号 * * @param key * @return * @throws Exception */ public long getNextCardNum(String key, Connection con) throws Exception { synchronized (obj) { long result = 0; int num = 0; if (key.length() == 1 && "2".equalsIgnoreCase(key)) { num = 10; } else if (key.length() == 7) { num = 4; } else if (key.length() == 11) { num = 4; key = key.substring(0, 7); } else { logger.error("aa"); throw new Exception("ss"); } long randomNumber = 0; int i = 0; String keyNum; do { i++; randomNumber = Random.getRandomNumber(num); keyNum = key + String.valueOf(randomNumber); logger.warn(i+ "aa" + keyNum); } while (checkNum(keyNum, con)); result = Long.parseLong(key + String.valueOf(randomNumber)); return result; } } /** * 通过查询数据库验证商城卡号是否存在 存在则返回true 反之返回false * * @param cardNum * @return */ private boolean checkNum(String cardNum, Connection con) throws Exception { boolean result = false; // CardMasterDao dao = new CardMasterDao(con); // List<CardMaster> cardList = dao // .findCardMaster("C_ID='" + cardNum + "'"); // if (cardList != null && cardList.size() > 0) { // logger.error(BusinessConfigParameters.W_ACTIVATE_CARD_002); // result = true; // } return result; } public static void main(String[] s) throws Exception { for (int i = 0; i < 10; i++) { System.out.println(CardIdManager.getInstance().getNextCardNum( "15868846882", null)); // System.out.println("======================"); System.out.println(CardIdManager.getInstance().getNextCardNum("2", null)); System.out.println("*******************************"); } } } import java.util.Date; /** * 随机数生成器 * * @author rq * */ public class Random { private static final String[] aStrRandom = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" }; /** * 获取num位随机数 * * @param num * @return */ public static long getRandomNumber(int num) { long result = 0; StringBuffer sb = new StringBuffer(); for (int i = 0; i < num; i++) { sb.append((int) (9 * Math.random() + 1)); } result = Long.parseLong(sb.toString()); return result; } public static String getRandomNumberStr(int num, boolean is, int nu) { String result = ""; StringBuffer sb = new StringBuffer(); int base = nu; for (int i = 0; i < num; i++) { if (is && nu == i) { sb.append(" "); nu = nu + base; } sb.append((int) (9 * Math.random() + 1)); } result = sb.toString(); return result; } /** * 获取随机数 * * @param base * @return */ public static int getRandom(int base) { return (int) (base * Math.random() + 1); } /** * 获取num位随机字符 * * @param num * @return */ public static String getRandomString(int num) { String result = ""; for (int i = 0; i < num; i++) { result += aStrRandom[getRandom(aStrRandom.length - 1)]; } return result; } public static void main(String[] q) throws Exception { // Date sDate = new Date(); // String filePath = "D://test//" + new Date().getTime(); // WorkBookManager work = WorkBookManager.getInstance(filePath); // work.initWorkBook(); // work.setMaxRow(1000); // work.setMaxSheetPage(2); // for (int i = 0; i < 1000; i++) { // String cardNum = Random.get3NumStr(String.valueOf(i + 1), 6); // String cardPwd = Random.getRandomNumberStr(17, true, 4); // work.put("1004 " + "001 " + "002 " + cardNum, cardPwd); // } // work.saveFile(); // // ZipUtils.put(filePath, filePath + ".zip"); // Date eDate = new Date(); // System.out // .println("共耗时=>" + (eDate.getTime() - sDate.getTime()) + "毫秒"); System.out.println(getSperator("123456dfvdfvdsavasdfasdf789", 4)); } /** * 获取线程睡眠时间 * * @param num * @return */ public static int getSleepNumber(int num) { return (int) (num * Math.random() + 1); } /** * @param s * @return */ public static String get3NumStr(String s, int num) { int temp = num - s.length(); for (int i = 0; i < temp; i++) { s = "0" + s; } return s; } public static String getSperator(String s, int num) { StringBuffer result = new StringBuffer(); int base = num; for (int i = 0; i < s.length(); i++) { if (i == base) { base += num; result.append(" "); } result.append(s.substring(i, i + 1)); } return result.toString(); } }
试试其它关键字
同步id生成器
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
贡献的其它代码
Label
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3