当前位置:网站首页>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
边栏推荐
- Introduction to protobuf
- Goland 调试go使用-大白记录
- Queue (C language / linked list)
- 面了一圈,整理了这套面试题。。
- RPC过程
- Using qlst excel file
- Anonymous type (c Guide Basics)
- How to encrypt devices under the interconnection of all things
- Stm32f103zet6 [development of standard library functions] - Introduction to library functions
- RCC introduction of Hal Library
猜你喜欢
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
程序,进程,线程;内存结构图;线程的创建和启动;Thread的常用方法
On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
作文以记之 ~ 二叉树的前序遍历
Use of Arthas in JVM tools
Flink SQL实现流批一体
Goland 调试go使用-大白记录
Failed to convert a NumPy array to a Tensor(Unsupported Object type int)
洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解
正点原子携手OneOS直播 OneOS系统教程全面上线
随机推荐
swagger文档导出自定义v2/api-docs拦截
Go语言自学系列 | golang结构体指针
After a circle, I sorted out this set of interview questions..
【深度好文】Flink SQL流批⼀体化技术详解(一)
Ear acupoint diagnosis and treatment essay 0421
Kubernetes如何使用harbor拉去私有镜像
[explanation] get ora-12838: cannot read / modify an object after modifying it in parallel
QT reading and writing XML files
An example of network communication based on TCP / IP protocol -- file transmission
tsdf +mvs
RPC过程
跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
Asan minimalism
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
rembg 分割mask
ajax防止缓存方法
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
匿名类型(C# 指南 基础知识)
洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置
分组背包呀