当前位置:网站首页>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
边栏推荐
- On traversal of binary tree
- Common programming records - parser = argparse ArgumentParser()
- 2. Average length of words
- PyTorch入门小笔记——利用简单例子观察前向传播各个层输出的size
- Shansi Valley P290 polymorphism exercise
- Pyqy5 learning (2): qmainwindow + QWidget + qlabel
- Three ways to create threads
- 4. Print form
- 去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention
- 数字图像处理基础(冈萨雷斯)二:灰度变换与空间滤波
猜你喜欢

Illustrate the significance of hashcode

Implementation of displaying database pictures to browser tables based on thymeleaf

Complete example demonstration of creating table to page - joint table query

Pytorch learning record (V): back propagation + gradient based optimizer (SGD, adagrad, rmsporp, Adam)
![去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention](/img/4e/1a51636853d11544e6f5c37a588730.png)
去噪论文阅读——[RIDNet, ICCV19]Real Image Denoising with Feature Attention

Fundamentals of digital image processing (Gonzalez) I

Preparedstatement prevents SQL injection

SQL injection

Pyqy5 learning (III): qlineedit + qtextedit
![如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation](/img/33/780b80693f70112eebc10941f7c134.png)
如何利用对比学习做无监督——[CVPR22]Deraining&[ECCV20]Image Translation
随机推荐
Latex quick start
图像恢复论文——[RED-Net, NIPS16]Image Restoration Using Very Deep Convolutional Encoder-Decoder Networks wi
IO multiplexing of 09 redis
JSP syntax and JSTL tag
Linear algebra Chapter 1 - determinant
Multithreading and high concurrency (2) -- detailed explanation of synchronized usage
线性代数第一章-行列式
Remedy after postfix becomes a spam transit station
编程记录——图片旋转函数scipy.ndimage.rotate()的简单使用和效果观察
7.Domino piling
深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
去噪论文阅读——[CVPR2022]Blind2Unblind: Self-Supervised Image Denoising with Visible Blind Spots
Fundamentals of SQL: first knowledge of database and SQL - installation and basic introduction - Alibaba cloud Tianchi
Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
Pytorch学习记录(四):参数初始化
Custom exception class
MySQL basic madness theory
Latex快速入门
Fundamentals of digital image processing (Gonzalez) II: gray transformation and spatial filtering
You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it