当前位置:网站首页>Basic usage of synchronized locks
Basic usage of synchronized locks
2022-04-23 08:39:00 【yuzhang_ zy】
1. Decorated code block , Specify the lock object , Lock a given object , Get the lock of the given object before entering the synchronization code block , And it needs to be the same object lock , That is, create an implementation Runnable The interface needs to be the same object , If two objects are created, they do not belong to the same object lock :
public class ThreadCount implements Runnable{
private static int count = 100;
@Override
public void run() {
while (true){
cal();
}
}
private void cal() {
if (count > 1){
// Lock
synchronized (this){
count--;
System.out.println(Thread.currentThread().getName() + " " + count);
}
}
}
public static void main(String[] args) {
ThreadCount threadCount = new ThreadCount();
new Thread(threadCount).start();
new Thread(threadCount).start();
}
}
2. Decorated instance method , Locks on the current instance , Obtain the lock for the current instance before entering the synchronized code :
package thread;
public class ThreadCount implements Runnable{
private static int count = 100;
@Override
public void run() {
while (true){
cal();
}
}
// If you will synchronized When added to the instance method, use this lock , Equivalent to the previous writing , Therefore, it is generally more convenient to lock the method
private synchronized void cal() {
if (count > 1){
count--;
System.out.println(Thread.currentThread().getName() + " " + count);
}
}
public static void main(String[] args) {
ThreadCount threadCount = new ThreadCount();
new Thread(threadCount).start();
new Thread(threadCount).start();
}
}
3. If you will synchronized Add to static methods , The current class name is used by default .class:
package thread;
public class ThreadCount implements Runnable{
private static int count = 100;
@Override
public void run() {
while (true){
cal();
}
}
// If you will synchronized Add to static methods , The current class name is used by default .class
private void cal() {
synchronized(ThreadCount.class){
if (count > 1){
count--;
System.out.println(Thread.currentThread().getName() + " " + count);
}
}
}
public static void main(String[] args) {
ThreadCount threadCount = new ThreadCount();
new Thread(threadCount).start();
new Thread(threadCount).start();
}
}
4. If we are using synchronized We need to pay attention to synchronized The problem of lock nesting avoids the occurrence of deadlock :
版权声明
本文为[yuzhang_ zy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230802300239.html
边栏推荐
猜你喜欢
PgSQL wants to implement all kinds of column sub query operations of MySQL
ELK生产实践
DJ音乐管理软件Pioneer DJ rekordbox
Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing
【路科V0】验证环境2——验证环境组件
面了一圈,整理了这套面试题。。
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
K210学习笔记(二) K210与STM32进行串口通信
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
随机推荐
Introduction to protobuf
DOM learning notes - traverse all element nodes of the page
分组背包呀
作文以记之 ~ 二叉树的后序遍历
LeetCode396.旋转数组
匿名类型(C# 指南 基础知识)
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
Redis Desktop Manager for Mac(Redis可视化工具)
After a circle, I sorted out this set of interview questions..
rembg 分割mask
synchronized 锁的基本用法
Enctype attribute in form
jsp页面编码
对OutputStream类的flush()方法的误解
Test your machine learning pipeline
Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board
Word plus watermark
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
rust 使用tokio的Notify 和timeout实现类似可超时条件变量的效果
Ajax cache prevention method