当前位置:网站首页>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
边栏推荐
- Complete example demonstration of creating table to page - joint table query
- 卡尔曼滤波与惯性组合导航
- Fundamentals of digital image processing (Gonzalez) I
- 2. Average length of words
- Paper on LDCT image reconstruction: edge enhancement based transformer for medical image denoising
- Treatment of tensorflow sequelae - simple example record torch utils. data. dataset. Picture dimension problem when rewriting dataset
- Graphic numpy array matrix
- PHP processing JSON_ Decode() parses JSON stringify
- Fact final variable and final variable
- container
猜你喜欢
Linear algebra Chapter 2 - matrices and their operations
Comparative study paper - [Moco, cvpr2020] momentum contract for unsupervised visual representation learning
CONDA virtual environment management (create, delete, clone, rename, export and import)
线性代数第三章-矩阵的初等变换与线性方程组
Pyqy5 learning (2): qmainwindow + QWidget + qlabel
Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
PyQy5学习(四):QAbstractButton+QRadioButton+QCheckBox
lambda expressions
Pytorch learning record (7): skills in processing data and training models
深入理解去噪论文——FFDNet和CBDNet中noise level与噪声方差之间的关系探索
随机推荐
7.Domino piling
Understanding and use of tp50, tp90 and tp99
Filebrowser realizes private network disk
EditorConfig
Pytorch学习记录(九):Pytorch中卷积神经网络
MySQL basic madness theory
Denoising paper - [noise2void, cvpr19] noise2void learning denoising from single noise images
Create enterprise mailbox account command
IO multiplexing of 09 redis
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
Gaussian processes of sklearn
SQL injection
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
Pytorch——数据加载和处理
数字图像处理基础(冈萨雷斯)二:灰度变换与空间滤波
The official website of UMI yarn create @ umijs / UMI app reports an error: the syntax of file name, directory name or volume label is incorrect
Graphic numpy array matrix
线性代数第三章-矩阵的初等变换与线性方程组
PHP processing JSON_ Decode() parses JSON stringify
Programming record - picture rotation function SciPy ndimage. Simple use and effect observation of rotate()