当前位置:网站首页>Detailed explanation of eight locks
Detailed explanation of eight locks
2022-06-22 06:17:00 【Kuxiaoya】
“ Eight locks ” What does it mean ?
Well understood. , Namely : of 8 Problems with locks !
Question 1 : In the standard case , Two threads print and send text messages first Or call ?
answer :
texting
Make a phone call
The code is as follows :
public class Demo1 {
public static void main(String[] args) {
Phone phone = new Phone();
// The existence of locks
new Thread(()-> {
phone.sendSms();
},"A").start();
// Capture
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()-> {
phone.call();
},"B").start();
}
}
class Phone{
//synchronized The object of the lock is the caller of the method !
// Both methods use the same lock , Who gets it first, who executes !
public synchronized void sendSms(){
System.out.println(" texting ");
}
public synchronized void call(){
System.out.println(" Make a phone call ");
}
}
Question two :sendSms Delay 4 second , Two threads print first texting Or call ?
answer :
(4 Seconds later , Show results )
texting
Make a phone call
public class Demo1 {
public static void main(String[] args) {
Phone phone = new Phone();
// The existence of locks
new Thread(()-> {
phone.sendSms();
},"A").start();
// Capture
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(()-> {
phone.call();
},"B").start();
}
}
class Phone{
//synchronized The object of the lock is the caller of the method !
// Both methods use the same lock , Who gets it first, who executes !
public synchronized void sendSms(){
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" texting ");
}
public synchronized void call(){
System.out.println(" Make a phone call ");
}
}
Question 3 : A synchronization method , A common method , Start by texting Or call ?
answer : ( First perform the normal method , Because it is not a synchronous method and is not affected by the lock , And send text messages to sleep 4 second , So texting must be behind her .)
Make a phone call
(4 Seconds later , Show )
texting
public class Demo2 {
public static void main(String[] args) {
Phone2 phone = new Phone2();
// The existence of locks
new Thread(() -> {
phone.sendSms();
}, "A").start();
// Capture
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
phone.call();
}, "B").start();
}
}
class Phone2 {
//synchronized The object of the lock is the caller of the method !
// Both methods use the same lock , Who gets it first, who executes !
public synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" texting ");
}
// There's no lock here ! It's not a synchronization method , Not affected by the lock !
public void call() {
System.out.println(" Make a phone call ");
}
}
Question 4 : Two objects , Two synchronization methods , Start by texting Or call ?
answer :( Call first , Because it's two locks , They don't influence each other , Then send a text message to sleep 4 second )
Make a phone call
(4 Seconds later , Show )
texting
public class Demo2 {
public static void main(String[] args) {
// Two objects , Two callers , Two locks !
Phone2 phone1 = new Phone2();
Phone2 phone2 = new Phone2();
// The existence of locks
new Thread(() -> {
phone1.sendSms();
}, "A").start();
// Capture
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
phone2.call();
}, "B").start();
}
}
class Phone2 {
//synchronized The object of the lock is the caller of the method !
// Both methods use the same lock , Who gets it first, who executes !
public synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" texting ");
}
public synchronized void call() {
System.out.println(" Make a phone call ");
}
}
Question five : Add two static synchronization methods , There is only one object , Print, text or call first ?
answer :( Because the same lock is used , Kind of lock , Who gets it first, who executes it first , So send a text message to get it first .
(4 Seconds later , Show )
texting
Make a phone call
public class Demo3 {
public static void main(String[] args) {
// two-object Class Class template has only one ,static , Locked Class
Phone3 phone = new Phone3();
// The existence of locks
new Thread(() -> {
phone.sendSms();
}, "A").start();
// Capture
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
phone.call();
}, "B").start();
}
}
//Phone3 The only one Class object
class Phone3 {
//synchronized The object of the lock is the caller of the method !
//static Static methods
// As long as the class is loaded ! Class Templates
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" texting ");
}
public static synchronized void call() {
System.out.println(" Make a phone call ");
}
}
Question 6 : Add two static synchronization methods , Two objects , Print, text or call first ?
answer :( Because the same lock is used , Kind of lock , Who gets it first, who executes it first , So send a text message to get it first .)
(4 Seconds later , Show )
texting
Make a phone call
public class Demo3 {
public static void main(String[] args) {
// two-object Class Class template has only one ,static , Locked Class
Phone3 phone1 = new Phone3();
Phone3 phone2 = new Phone3();
// The existence of locks
new Thread(() -> {
phone1.sendSms();
}, "A").start();
// Capture
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
phone2.call();
}, "B").start();
}
}
//Phone3 The only one Class object
class Phone3 {
//synchronized The object of the lock is the caller of the method !
//static Static methods
// As long as the class is loaded ! Class Templates
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" texting ");
}
public static synchronized void call() {
System.out.println(" Make a phone call ");
}
}
Question seven : A static synchronization method , A common synchronization method , An object ; Print and send text messages or make phone calls first ?
answer :( Because they don't use a lock , One is class lock , One is the caller of the lock .)
Make a phone call
(4 Seconds later , Show )
texting
public class Demo4 {
public static void main(String[] args) {
// two-object Class Class template has only one ,static , Locked Class
Phone4 phone = new Phone4();
// The existence of locks
new Thread(() -> {
phone.sendSms();
}, "A").start();
// Capture
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
phone.call();
}, "B").start();
}
}
//Phone3 The only one Class object
class Phone4 {
// Static synchronization method
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" texting ");
}
// Common synchronization methods
public synchronized void call() {
System.out.println(" Make a phone call ");
}
}
Question 8 : A static synchronization method , A common synchronization method , Two objects ; Print and send text messages or make phone calls first ?
answer :( Because they don't use a lock , One is class lock , One is the caller of the lock .)
Make a phone call
(4 Seconds later , Show )
texting
public class Demo4 {
public static void main(String[] args) {
// two-object Class Class template has only one ,static , Locked Class
Phone4 phone1 = new Phone4();
Phone4 phone2 = new Phone4();
// The existence of locks
new Thread(() -> {
phone1.sendSms();
}, "A").start();
// Capture
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
phone2.call();
}, "B").start();
}
}
//Phone3 The only one Class object
class Phone4 {
// Static synchronization method
public static synchronized void sendSms() {
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" texting ");
}
// Common synchronization methods
public synchronized void call() {
System.out.println(" Make a phone call ");
}
}
边栏推荐
- [Key review of cloud computing]
- Shengxin visualization (Part3) -- violin diagram
- Oracle之trim,ltrim,rtrim三个函数的用法
- 上传文件提示 413 Request Entity Too Large 错误
- Single cell literature learning (Part2) -- stplus: a reference based method for the exact enhancement of St
- 关于jinja2 宏定义的小问题
- 【Rust笔记】01-基本类型
- Discrete PID control based on MATLAB
- Vertical maximum and minimum and horizontal maximum and minimum greatest(), least(), max(), min()
- 生信文献学习(part1)--PRECISE: a ... approach to transfer predictors of drug response from pre-clinical ...
猜你喜欢

Little bear school bearpi HM micro officially integrated into openharmony trunk

Callable

雷达导引头伺服系统的建模与仿真

不务正业系列7:老照片去除斑点手法

常用的辅助类—(重点)

生信可视化(part3)--小提琴图

Single cell paper record (Part11) -- clustermap for multi-scale clustering analysis of spatial gene expression

ReadWriteLock

Single cell literature learning (Part2) -- stplus: a reference based method for the exact enhancement of St

SQL injection vulnerability (XIV) XFF injection attack
随机推荐
上传文件提示 413 Request Entity Too Large 错误
Geoswath plus technology and data acquisition and processing
Pyg tutorial (7): dissecting neighborhood aggregation
Idea run scope locally
[NAND file system] UBI introduction
Single cell paper record (Part11) -- clustermap for multi-scale clustering analysis of spatial gene expression
Single cell thesis record (part13) -- spagcn: integrating gene expression, spatial location and history to
On the definition of jinja2 macro
牛客-TOP101-BM27
Markdown中插入类图(classDiagram)
Upload file prompt 413 request entity too large error
Current harmonic suppression strategy of grid connected inverter with PIR controller regulator
八锁问题详解
leetcode每周3道(八)图之最短路
【NAND文件系统】UBI介绍
IO密集型和CPU密集型
基于卫星测深的牙买加沿岸水深测量
e.hash & oldCap == 0 详细解读
ReadWriteLock
Vulnérabilité à l'injection SQL (XIII) injection base64