当前位置:网站首页>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
边栏推荐
- Problems and solutions of database migration
- Reading of denoising papers - [cvpr2022] blind2blind: self supervised image denoising with visible blind spots
- Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
- Viewer: introduce MySQL date function
- 深度学习基础——简单了解meta learning(来自李宏毅课程笔记)
- Pytorch learning record (XII): learning rate attenuation + regularization
- Framework analysis 1 Introduction to system architecture
- Pyqy5 learning (III): qlineedit + qtextedit
- Treatment of tensorflow sequelae - simple example record torch utils. data. dataset. Picture dimension problem when rewriting dataset
- Algèbre linéaire chapitre 2 - matrice et son fonctionnement
猜你喜欢

String notes

Filebrowser realizes private network disk

Pytorch notes - complete code for linear regression & manual or automatic calculation of gradient code comparison

CONDA virtual environment management (create, delete, clone, rename, export and import)

SQL injection
![Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising](/img/cd/10793445e6867eeee613b6ba4b85cf.png)
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising

You cannot access this shared folder because your organization's security policy prevents unauthenticated guests from accessing it

線性代數第二章-矩陣及其運算

lambda expressions

Pytoch learning record (x): data preprocessing + batch normalization (BN)
随机推荐
SQL injection
Solution record of slow access speed of SMB service in redhat6
List segmentation best practices
Shansi Valley P290 polymorphism exercise
Anaconda安装PyQt5 和 pyqt5-tools后没有出现designer.exe的问题解决
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation
Pytorch学习记录(十三):循环神经网络((Recurrent Neural Network)
Get the value of state in effects in DVA
SQL optimization best practices
Pytorch学习记录(十):数据预处理+Batch Normalization批处理(BN)
Opensips (1) -- detailed process of installing opensips
Fundamentals of in-depth learning -- a simple understanding of meta learning (from Li Hongyi's course notes)
In depth source code analysis servlet first program
PyQy5学习(三):QLineEdit+QTextEdit
Filebrowser realizes private network disk
線性代數第二章-矩陣及其運算
JSP syntax and JSTL tag
线性代数第二章-矩阵及其运算
DBCP usage
在Jupyter notebook中用matplotlib.pyplot出现服务器挂掉、崩溃的问题