当前位置:网站首页>A single node obtains the lock lock of the order number
A single node obtains the lock lock of the order number
2022-06-28 07:35:00 【Huangyuewang】
effect :
100 Threads get the order number at the same time

1, establish OrderService class
package com.hyw.service;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class OrderService implements Runnable {
static Integer number = 50000000; // Initialization order No
private Lock lock = new ReentrantLock();
@Override
public void run() {
getNumber();
}
private void getNumber(){
try {
lock.lock();
String number = generatorNumber();
System.out.println(Thread.currentThread()+", Acquired number:"+number);
} finally {
lock.unlock();
}
}
/**
* Generate order number
* @return
*/
static String generatorNumber(){
String dh = " Odd Numbers :"+number;
number ++;
return dh;
}
}
2. simulation 100 Threads generate order numbers
package com.hyw.lock;
import com.hyw.service.OrderService;
public class TestMain {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
new Thread(new OrderService()).start();
}
}
}Running results :

边栏推荐
- Kubelet garbage collection (exiting containers and unused images) source code analysis
- 代码提交规范
- DOM parsing of XML file case code sentence by sentence analysis
- QT -- 通讯协议
- 东方财富上开户是安全的吗
- Ice, protobuf, thrift -- Notes
- kubelet驱逐机制的源码分析
- Real time database - Notes
- kubernetes部署thanos ruler的发送重复告警的一个隐秘的坑
- MySQL master-slave replication, detailed configuration, create unable to connect processing prompt
猜你喜欢

goland IDE和delve调试位于kubernetes集群中的go程序

一个小工具可以更快的写爬虫

Construction and exploration of vivo database and storage platform

MySQL installation steps - installing MySQL on Linux (3)

Pfizer's new Guankou medicine has entered the Chinese market, and the listing of relevant products of domestic pharmaceutical enterprises is just around the corner

The practice of traffic and data isolation in vivo Reviews

No suspense about the No. 1 Internet company overtime table

Cloud native (to be updated)

Hack the box:routerspace

看似简单的光耦电路,实际使用中应该注意些什么?
随机推荐
卸载重装最新版mysql数据库亲测有效
Drawing animated bubble chart with R language
MySQL installation steps - Linux configuration file JDK installation (II)
Introduction and several months' experience of extending the solution thanos of Prometheus
Cloud native (to be updated)
在idea中,get和set方法爆红可能是没有安装Lombok插件
ABAP 技能树
goland IDE和delve调试位于kubernetes集群中的go程序
DOM parsing of XML file case code sentence by sentence analysis
数字藏品市场“三大套路”
Application of XOR. (extract the rightmost 1 in the number, which is often used in interviews)
异或的应用。(提取出数字中最右侧的1,面试中经常用的到)
linux下修改mysql用户名root
Block transmission by golang gin framework
Sword finger offer|: linked list (simple)
The practice of event driven architecture in vivo content platform
NDK 交叉编译
Implementation of commit message standardized control in large projects
Application and Optimization Practice of redis in vivo push platform
What should I do if the version is incompatible with the jar package conflict?