当前位置:网站首页>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
边栏推荐
猜你喜欢

Reference passing 1

Knowledge points and problem solutions related to information collection

Overview of bus structure

Yangtao electronic STM32 Internet of things introduction 30 steps notes 1. The difference between Hal library and standard library

经典题目刷一刷

Redis Desktop Manager for Mac(Redis可视化工具)
![[C语言] 文件操作《一》](/img/89/b19dda13d27e37fedf6736c102245b.png)
[C语言] 文件操作《一》

Virtual online exhibition - Online VR exhibition hall realizes 24h immersive exhibition viewing

Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam

SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
随机推荐
Go语言自学系列 | golang方法
【精品】利用动态代理实现事务统一管理 二
使用flask和h5搭建网站/应用的简要步骤
二维01背包
DJ音乐管理软件Pioneer DJ rekordbox
【路科V0】验证环境2——验证环境组件
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
ESP32程序下载失败,提示超时
JSP page coding
Use of Arthas in JVM tools
【深度好文】Flink SQL流批⼀体化技术详解(一)
Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
作文以记之 ~ 二叉树的后序遍历
分组背包呀
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
LINQ学习系列-----1.4 匿名对象
pdf加水印
IDEA导入commons-logging-1.2.jar包
ajax防止缓存方法
经典题目刷一刷