当前位置:网站首页>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
边栏推荐
- An example of network communication based on TCP / IP protocol -- file transmission
- xctf刷题小记
- Notes on 30 steps of introduction to Internet of things of yangtao electronics STM32 III. Explanation of new cubeide project and setting
- Notes on English class (4)
- Yangtao electronic STM32 Internet of things entry 30 step notes II. Cube ide download, installation, sinicization and setting
- 【精品】利用动态代理实现事务统一管理 二
- SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
- QFileDialog select multiple files or folders
- Excle plus watermark
- Overview of bus structure
猜你喜欢

LeetCode-199-二叉树的右视图

pgsql想实现mysql一样样的列子查询操作

Idea import commons-logging-1.2 Jar package

vmware 搭建ES8的常见错误

四张图弄懂matplotlib的一些基本用法

Qtablewidget header customization and beautification developed by pyqt5 (with source code download)

Get the absolute path of the class according to the bytecode

'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...

PgSQL wants to implement all kinds of column sub query operations of MySQL

MySQL查询两张表属性值非重复的数据
随机推荐
tsdf +mvs
Go语言自学系列 | golang嵌套结构体
Asan minimalism
Noyer électronique stm32 Introduction à l'Internet des objets 30 étapes notes I. différences entre la Bibliothèque Hal et la Bibliothèque standard
An example of network communication based on TCP / IP protocol -- file transmission
STM32F103ZET6【标准库函数开发】----库函数介绍
LeetCode396.旋转数组
Use of Arthas in JVM tools
word加水印
Redis master-slave server problem
一个必看的微信小程序开发指南1-基础知识了解
JSP page coding
Copy array in JS
线程的调度(优先级)
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
怎样读取Excel表格到数据库
QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
input元素添加监听事件
Ear acupoint diagnosis and treatment essay 0421
分组背包呀