当前位置:网站首页>Example of ticket selling with reentrant lock
Example of ticket selling with reentrant lock
2022-04-23 06:09:00 【linsa_ pursuer】
1.synchronized
package juc.one;
class Ticket {
// Number of votes
private int number = 30;
// Operation method : Selling tickets
public synchronized void sale() {
// Judge : Do you have a ticket?
if(number > 0){
System.out.println(Thread.currentThread().getName()+" : sell :"+(number--)+" be left over :"+number);
}
}
}
public class SaleTicket {
// The second step Create multiple threads , Call the operation method of the resource class
public static void main(String[] args) {
// establish Ticket object
Ticket ticket = new Ticket();
// Create three threads
new Thread(new Runnable() {
@Override
public void run() {
// Call ticket selling method
for(int i=0;i<40;i++){
ticket.sale();
}
}
},"AA").start();
new Thread(new Runnable() {
@Override
public void run() {
// Call ticket selling method
for(int i=0;i<40;i++){
ticket.sale();
}
}
},"BB").start();
new Thread(new Runnable() {
@Override
public void run() {
// Call ticket selling method
for(int i=0;i<40;i++){
ticket.sale();
}
}
},"CC").start();
}
}
2.ReentrantLock
package juc.two;
import java.util.concurrent.locks.ReentrantLock;
// First step Create a resource class , Define properties and operation methods
class LTicket {
// Number of tickets
private int number = 30;
// Create a reentrant lock
private final ReentrantLock lock = new ReentrantLock();
// Ticket selling method
public void sale() {
// locked
lock.lock();
// Judge whether there is a ticket
if(number > 0){
System.out.println(Thread.currentThread().getName()+" : sell "+(number--)+" The remaining :"+number);
}
// Unlock
lock.unlock();
}
}
public class LSaleTicket {
// The second step Create multiple threads , Call the operation method of the resource class
// Create three threads
public static void main(String[] args) {
LTicket ticket = new LTicket();
new Thread(()->{
for(int i = 0; i < 40; i++){
ticket.sale();
}
},"AA").start();
new Thread(()->{
for(int i = 0; i < 40; i++){
ticket.sale();
}
},"BB").start();
new Thread(()->{
for(int i = 0; i < 40; i++){
ticket.sale();
}
},"CC").start();
}
}
版权声明
本文为[linsa_ pursuer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220533259447.html
边栏推荐
- Pyqy5 learning (4): qabstractbutton + qradiobutton + qcheckbox
- Pytorch学习记录(五):反向传播+基于梯度的优化器(SGD,Adagrad,RMSporp,Adam)
- 编写一个自己的 RedisTemplate
- String notes
- Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
- Pytorch学习记录(十二):学习率衰减+正则化
- Contrôle automatique (version Han min)
- Pytoch learning record (x): data preprocessing + batch normalization (BN)
- Explain of MySQL optimization
- Create enterprise mailbox account command
猜你喜欢
Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
Pyemd installation and simple use
Chapter 3 of linear algebra - Elementary Transformation of matrix and system of linear equations
Generate excel template (drop-down selection, multi-level linkage)
深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
Linear algebra Chapter 2 - matrices and their operations
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
Pytoch learning record (x): data preprocessing + batch normalization (BN)
随机推荐
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
Numpy common function table sorting of data processing
A general U-shaped transformer for image restoration
Pytorch学习记录(七):处理数据和训练模型的技巧
sklearn之 Gaussian Processes
Pytorch学习记录(九):Pytorch中卷积神经网络
Multithreading and high concurrency (2) -- detailed explanation of synchronized usage
无监督去噪——[TMI2022]ISCL: Interdependent Self-Cooperative Learning for Unpaired Image Denoising
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
Pytorch——数据加载和处理
Pytorch学习记录(十):数据预处理+Batch Normalization批处理(BN)
Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison
Pytorch learning record (XI): data enhancement, torchvision Explanation of various functions of transforms
RedHat realizes keyword search in specific text types under the directory and keyword search under VIM mode
lambda expressions
PyTorch笔记——观察DataLoader&用torch构建LeNet处理CIFAR-10完整代码
Gaussian processes of sklearn
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
Fundamentals of SQL: first knowledge of database and SQL - installation and basic introduction - Alibaba cloud Tianchi
Preparedstatement prevents SQL injection