Ai
1 Star 0 Fork 59

Yangjianwei/java-performance

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
IDSeqHelper.java 794 Bytes
一键复制 编辑 原始数据 按行查看 历史
xiandafu 提交于 2019-11-12 14:15 +08:00 . ”test“
package com.ibeetl.code.ch03;
import java.util.concurrent.TimeUnit;
/**
* 演示了不加锁操作的计数{@code IDSeqHelper}会出现各种问题,
* @author 公众号 java系统优化
*/
public class IDSeqHelper {
static int a = 0;
public static int add(){
a++;
return a;
}
public static void main(String[] args){
/**
* 俩个并发线程
*/
CountThread aThread = new CountThread("a");
CountThread bThread = new CountThread("b");
aThread.start();
bThread.start();
}
static class CountThread extends Thread{
int last = 0;
public CountThread(String name){
super(name);
}
public void run(){
while(true){
int a = IDSeqHelper.add();
if(a==last){
System.out.println("error "+a);
}
last = a;
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/higher/java-performance.git
git@gitee.com:higher/java-performance.git
higher
java-performance
java-performance
master

搜索帮助