当前位置:网站首页>04 traditional synchronized lock
04 traditional synchronized lock
2022-07-26 23:37:00 【Casey·Hu】
3、Lock lock ( a key )
In the traditional way synchronized
04- Conventional Synchronized lock
1. An example of buying a ticket
package com.hkx.demo01;
/** * @program: juc * @description: An example of buying a ticket * @author: Casey Hu * @create: 2022-07-24 19:11 **/
/*** * True multithreaded development Development in the company * Thread is a separate resource class , There is no ancillary operation * 1. attribute Method */
public class SaleTicketDemo01 {
public static void main(String[] args) {
new Thread(new MyThread()).start();
}
}
class MyThread implements Runnable{
@Override
public void run(){
}
}
2. Thread is a separate resource class , There is no ancillary operation
Runnable() Belong to : Functional interface @FunctionalInterface
Anonymous inner class new Runable()
package com.hkx.demo01;
/** * @program: juc * @description: An example of buying a ticket * @author: Casey Hu * @create: 2022-07-24 19:11 **/
/*** * True multithreaded development Development in the company * Thread is a separate resource class , There is no ancillary operation * 1. attribute Method */
public class SaleTicketDemo01 {
public static void main(String[] args) {
// Concurrent : Multiple threads operate on the same resource class , Drop resource class into thread
Ticket ticket=new Ticket(); // Resource class
// Functional interface @FunctionalInterface ,jdk1.8 after lambda expression ( Parameters )->{ Code }
new Thread(()-> {
// Three threads Methods of operating resources
for (int i=0;i<60;i++){
ticket.sale();
}
},"A").start();
new Thread(()-> {
for (int i=0;i<60;i++){
ticket.sale();
}
},"B").start();
new Thread(()-> {
for (int i=0;i<60;i++){
ticket.sale();
}
},"C").start();
}
}
// Resource class OOP Development
class Ticket{
// attribute and Method
private int number=50;
// How to buy tickets
// synchronized The essence : queue lock
public synchronized void sale(){
if (number>0){
System.out.println(Thread.currentThread().getName()+" Sold No "+(number--)+" Tickets , The remaining :"+number);
}
}
}
Output log :
"D:\Program Files (x86)\Java\jdk1.8.0_144\bin\java.exe" "-javaagent:D:\Program Files (x86)\JetBrains\IntelliJ IDEA 2022.1.3\lib\idea_rt.jar=58815:D:\Program Files (x86)\JetBrains\IntelliJ IDEA 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\charsets.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\deploy.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\javaws.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\jce.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\jfr.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\jsse.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\management-agent.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\plugin.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\resources.jar;D:\Program Files (x86)\Java\jdk1.8.0_144\jre\lib\rt.jar;E:\IdeaProjects\juc\juc\target\classes;D:\Maven\repository\org\projectlombok\lombok\1.18.8\lombok-1.18.8.jar" com.hkx.demo01.SaleTicketDemo01
A Sold No 50 Tickets , The remaining :49
A Sold No 49 Tickets , The remaining :48
A Sold No 48 Tickets , The remaining :47
B Sold No 47 Tickets , The remaining :46
B Sold No 45 Tickets , The remaining :44
B Sold No 44 Tickets , The remaining :43
A Sold No 46 Tickets , The remaining :45
A Sold No 42 Tickets , The remaining :41
A Sold No 41 Tickets , The remaining :40
A Sold No 40 Tickets , The remaining :39
A Sold No 39 Tickets , The remaining :38
A Sold No 38 Tickets , The remaining :37
B Sold No 43 Tickets , The remaining :42
B Sold No 35 Tickets , The remaining :34
B Sold No 34 Tickets , The remaining :33
B Sold No 33 Tickets , The remaining :32
B Sold No 32 Tickets , The remaining :31
B Sold No 31 Tickets , The remaining :30
B Sold No 30 Tickets , The remaining :29
B Sold No 29 Tickets , The remaining :28
B Sold No 28 Tickets , The remaining :27
B Sold No 27 Tickets , The remaining :26
B Sold No 26 Tickets , The remaining :25
B Sold No 25 Tickets , The remaining :24
B Sold No 24 Tickets , The remaining :23
B Sold No 23 Tickets , The remaining :22
B Sold No 22 Tickets , The remaining :21
B Sold No 21 Tickets , The remaining :20
B Sold No 20 Tickets , The remaining :19
B Sold No 19 Tickets , The remaining :18
B Sold No 18 Tickets , The remaining :17
B Sold No 17 Tickets , The remaining :16
B Sold No 16 Tickets , The remaining :15
B Sold No 15 Tickets , The remaining :14
B Sold No 14 Tickets , The remaining :13
B Sold No 13 Tickets , The remaining :12
B Sold No 12 Tickets , The remaining :11
A Sold No 36 Tickets , The remaining :35
A Sold No 10 Tickets , The remaining :9
A Sold No 9 Tickets , The remaining :8
A Sold No 8 Tickets , The remaining :7
A Sold No 7 Tickets , The remaining :6
A Sold No 6 Tickets , The remaining :5
A Sold No 5 Tickets , The remaining :4
A Sold No 4 Tickets , The remaining :3
A Sold No 3 Tickets , The remaining :2
A Sold No 2 Tickets , The remaining :1
A Sold No 1 Tickets , The remaining :0
C Sold No 37 Tickets , The remaining :36
B Sold No 11 Tickets , The remaining :10
Process ended , Exit code 0
Lock lock 
Lock Interface

Lock The three implementation classes of the interface 
SaleTicketDemo02

Fair lock : Processing thread fairness , first come , first served
Not fair lock : Processing threads are unfair , according to CPU Resources can jump the queue ( Default unfair lock )
package com.hkx.demo01;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/** * @program: juc * @description: An example of selling tickets * @author: Casey Hu * @create: 2022-07-25 21:55 **/
public class SaleTicketDemo02 {
public static void main(String[] args) {
// Concurrent : Multiple threads operate on the same resource class , Drop resource class into thread
Ticket2 ticket = new Ticket2(); // Resource class
new Thread(() -> {
for (int i = 0; i < 60; i++) ticket.sale();
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 60; i++) ticket.sale();
}, "B").start();
new Thread(() -> {
for (int i = 0; i < 60; i++) ticket.sale();
}, "C").start();
}
}
//Lock
class Ticket2 {
private int number = 50;
Lock lock = new ReentrantLock();
/*** * 1.new ReentrantLock() * 2.lock(); Lock * 3.unlock(); Unlock */
public void sale() {
lock.lock(); // Lock
try {
// Business code
if (number > 0) {
System.out.println(Thread.currentThread().getName() + " Sold No " + (number--) + " Tickets , The remaining :" + number);
}
} finally {
lock.unlock(); // Unlock
}
}
}
synchronized and Lock What's the difference
边栏推荐
- An online accident, I suddenly realized the essence of asynchrony
- 【不积跬步无以至千里】统计日志指定时间段内的关键词
- Hcia-r & s self use notes (18) campus network architecture foundation, switch working principle, VLAN principle
- Disk expansion process and problems encountered in the virtual machine created by VMWare
- Typescript stage learning
- Go uses flag package to parse command line parameters
- Part II - C language improvement_ 13. Recursive function
- TypeScript阶段学习
- C language dynamic memory management
- [Luogu] p1395 meeting
猜你喜欢

Pyqt5 how to set pushbutton click event to obtain file address

An online accident, I suddenly realized the essence of asynchrony

Three effective strategies for the transformation of data supply chain to be coordinated and successful

Hcia-r & s self use notes (23) DHCP

【2016】【论文笔记】差频可调谐THz技术——

Part II - C language improvement_ 12. Packaging and use of dynamic / precision Library

Too busy with scientific research to take care of your family? Chen Ting: life cannot have only one fulcrum

如何使用数据管道实现测试现代化

org.yaml.snakeyaml.scanner. ScannerException: mapping values are not allowed here in ‘reader‘, line

Cheaper than seals, with a large space for shape explosion. Is there really no match for 200000 or so? Chang'an's new "King fried" is cost-effective
随机推荐
The NFT market pattern has not changed. Can okaleido set off a new round of waves?
Esmfold: a new breakthrough in protein structure prediction after alphafold2
Part II - C language improvement_ 11. Pretreatment
Practical project: boost search engine
公有云安全性和合规性方面的考虑事项
[Luogu] p2709 little B's inquiry
The interviewer asked: this point of JS
MySQL 数据的导入
Typescript notes
What is the reason for oom during redis shake synchronization in shake database?
Science | 华盛顿大学利用AI和结构预测设计全新蛋白质
HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由
Novice online interview [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
Learn various details and thoughts of chatroom implementation in Muduo
Basic operations of objects
[H5 bottom scrolling paging loading]
C language dynamic memory management
告别宽表,用 DQL 成就新一代 BI
At 12:00 on July 17, 2022, the departure of love life on June 28 was basically completed, and it needs to rebound
[shaders realize distorted outline effect _shader effect Chapter 2]