- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我之前已经问过这个如何生成自增ID Generate auto increment number by using Java .
我使用了以下代码:
private static final AtomicInteger count = new AtomicInteger(0);
uniqueID = count.incrementAndGet();
之前的代码工作正常,但问题是 count
静态变量。对于这个静态,它永远不会再从 0
开始,它总是从最后一个增量 id 开始。这就是问题所在。
除了AtomicInteger
之外还有其他方法吗?
另一个问题是我正在使用 GWT,因此 AtomicInteger
在 GWT 中不可用。
所以我必须找到另一种方法来做到这一点。
最佳答案
AtomicInteger
是一个“有符号”整数。它将增加直到Integer.MAX_VALUE
;然后,由于整数溢出,您期望得到 Integer.MIN_VALUE
。
不幸的是,AtomicInteger
中的大多数线程安全方法都是最终的,包括 incrementAndGet()
,因此您无法覆盖它们。
但是您可以创建一个包装 AtomicInteger 的自定义类,并且只需根据您的需要创建同步
方法即可。例如:
public class PositiveAtomicInteger {
private AtomicInteger value;
//plz add additional checks if you always want to start from value>=0
public PositiveAtomicInteger(int value) {
this.value = new AtomicInteger(value);
}
public synchronized int incrementAndGet() {
int result = value.incrementAndGet();
//in case of integer overflow
if (result < 0) {
value.set(0);
return 0;
}
return result;
}
}
关于java - java生成自增id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35146440/
1、修改数据 复制代码代码如下: DataRow dr =
数据库操纵基本流程为: 1、连接数据库服务器 2、选择数据库 3、执行SQL语句 4、处理结果集 5、打印操作信息 其中用到的相关函数有 •resource m
CRUD是Create(创建)、Read(读取)、Update(更新)和Delete(删除)的缩写,它是普通应用程序的缩影。如果您掌握了某框架的CRUD编写,那么意味可以使用该框架创建普通应用程序了
项目结构: 添加页面: &
本文实例讲述了android操作sqlite数据库(增、删、改、查、分页等)及listview显示数据的方法。分享给大家供大家参考,具体如下: 由于刚接触android开发,故此想把学到的基础知识
我是一名优秀的程序员,十分优秀!