当前位置:网站首页>synchronized 锁的基本用法
synchronized 锁的基本用法
2022-04-23 08:02:00 【yuzhang_zy】
1. 修饰代码块,指定加锁对象,对给定对象加锁,进入同步代码块前要获得给定对象的锁,并且是在需要是同一个对象锁,也即创建实现Runnable接口的时候需要是同一个对象,如果创建了两个对象那么他们不属于同一个对象锁:
public class ThreadCount implements Runnable{
private static int count = 100;
@Override
public void run() {
while (true){
cal();
}
}
private void cal() {
if (count > 1){
// 加锁
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. 修饰实例方法,作用于当前实例加锁,进入同步代码前要获得当前实例的锁:
package thread;
public class ThreadCount implements Runnable{
private static int count = 100;
@Override
public void run() {
while (true){
cal();
}
}
// 如果将synchronized加到实例方法上则使用this锁, 等同于之前的写法, 所以一般在方法上加上锁会比较方便一点
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. 如果将synchronized加到静态方法上,默认使用当前的类名.class:
package thread;
public class ThreadCount implements Runnable{
private static int count = 100;
@Override
public void run() {
while (true){
cal();
}
}
// 如果将synchronized加到静态方法上,默认使用当前的类名.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. 我们如果在使用 synchronized 需要注意 synchronized 锁嵌套的问题避免死锁的问题发生:
版权声明
本文为[yuzhang_zy]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_39445165/article/details/124356384
边栏推荐
猜你喜欢
[C语言] 文件操作《一》
Online yaml to XML tool
Idea: export Yapi interface using easyyapi plug-in
让地球少些“碳”息 度能在路上
Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast
PyQt5开发之QTableWidget表头自定义与美化(附源代码下载)
clang 如何产生汇编文件
396. Rotate Function
Let the earth have less "carbon" and rest on the road
ASAN 极简原理
随机推荐
How to encrypt devices under the interconnection of all things
Rearranging log files for leetcode simple question
通过实现参数解析器HandlerMethodArgumentResolver接口来自定义注解
Common regular expressions
【学习】从零开始的音视频开发(9)——NuPlayer
QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
Data deletion and modification (MySQL)
Transformer-XL: Attentive Language ModelsBeyond a Fixed-Length Context 论文总结
QFileDialog select multiple files or folders
耳穴减肥自身感受细节描述0422
Knowledge points and problem solutions related to information collection
excle加水印
Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
LeetCode簡單題之計算字符串的數字和
There are some problems when using numeric type to query string type fields in MySQL
DOM 学习之—添加+-按钮
RPC procedure
获取TrustedInstaller权限
跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
vslam PPT