当前位置:网站首页>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 :

边栏推荐
- 面经---测试工程师web端自动化---大厂面试题
- R 语言 Hitters 数据分析
- Design and implementation of spark offline development framework
- What is the lifecycle of automated testing?
- okcc呼叫中心没有电脑的坐席能不能开展工作?
- Understanding of OPC protocol
- 卸载重装最新版mysql数据库亲测有效
- The practice of event driven architecture in vivo content platform
- 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
- Force buckle 515 Find the maximum value in each tree row
猜你喜欢

What is EC blower fan?

How bacnet/ip gateway collects data of building centralized control system

SQL statement optimization steps (1)

kubernetes删除pod的流程的源码简析

Tencent continued to lay off staff in the second half of the year, and all business groups reduced by at least 10%. What do you think of this? Followers

在idea中,get和set方法爆红可能是没有安装Lombok插件

以动态规划的方式求解最长回文子串

Mysql57 zip file installation

Kubelet garbage collection (exiting containers and unused images) source code analysis

2021 VDC: technological architecture evolution of vivo Internet service for 100 million users | PPT download attached
随机推荐
Makefile
QT -- communication protocol
Ice, protobuf, thrift -- Notes
linux下修改mysql端口号
分析 NFT 项目的 5 个指标
Top 25 most popular articles on vivo Internet technology in 2021
Kubernetes deploys a secret pit where thanos ruler sends repeated alarms
大型项目中的Commit Message规范化控制实现
8 张图 | 剖析 Eureka 的首次同步注册表
Design and implementation of spark offline development framework
Understanding of OPC protocol
MMR重排(相似度通过编辑距离和重复度计算)
R 语言 ggmap
自律挑战30天
DOM parsing of XML file case code sentence by sentence analysis
剑指offer II 091.粉刷房子
Reading notes - MySQL technology l act: InnoDB storage engine (version 2)
[ thanos源码分析系列 ]thanos query组件源码简析
以动态规划的方式求解最长回文子串
7-1 understand everything