当前位置:网站首页>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
边栏推荐
- Trust uses Tokio's notify and timeout to achieve the effect similar to the timeout condition variable
- After a circle, I sorted out this set of interview questions..
- JS中复制数组
- Introduction to protobuf
- vmware 搭建ES8的常见错误
- 经典题目刷一刷
- 求简单类型的矩阵和
- QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
- Using qlst excel file
- 对li类数组对象随机添加特性,并进行排序
猜你喜欢
面了一圈,整理了这套面试题。。
洋桃电子STM32物联网入门30步笔记三、CubeMX图形化编程、设置开发板上的IO口
QT reading and writing XML files
Excle plus watermark
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
STM32使用HAL库,整体结构和函数原理介绍
Reference passing 1
使用flask和h5搭建网站/应用的简要步骤
Shell script advanced
随机推荐
Get the absolute path of the class according to the bytecode
关于数组复制问题
An example of network communication based on TCP / IP protocol -- file transmission
ESP32程序下载失败,提示超时
Shell脚本进阶
DOM 学习之—添加+-按钮
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
Trust uses Tokio's notify and timeout to achieve the effect similar to the timeout condition variable
根据字节码获取类的绝对路径
耳穴诊疗随笔0421
Harbor企业级镜像管理系统实战
Use of applicationreadyevent
excle加水印
QT compilation qtxlsx Library
HAL库的RCC简介
二维01背包
第一性原理 思维导图
How much inventory recording does the intelligent system of external call system of okcc call center need?
JSP page coding
form表单 post提交 数据量大的问题