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

Community group purchase applet source code + interface DIY + nearby leader + supplier + group collage + recipe + second kill + pre-sale + distribution + live broadcast

Install MySQL for Ubuntu and query the average score

A simple theme of Typecho with beautiful appearance_ Scarfskin source code download

总线结构概述

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

'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。

ansible自動化運維詳解(一)ansible的安裝部署、參數使用、清單管理、配置文件參數及用戶級ansible操作環境構建

LeetCode简单题之重新排列日志文件

5.6 comprehensive case - RTU-

如何保护开源项目免遭供应链攻击-安全设计(1)
随机推荐
5.6 综合案例-RTU-
一个必看的微信小程序开发指南1-基础知识了解
数据可视化:使用Excel制作雷达图
输入/输出系统
耳穴诊疗随笔0421
刨析——浏览器如何工作
[C语言] 文件操作《一》
How to generate assembly file
colorui 解决底部导航遮挡内容问题
Sword finger offer Day24 math (medium)
如何保护开源项目免遭供应链攻击-安全设计(1)
Data deletion and modification (MySQL)
微信小程序 catchtap=“toDetail“ 事件问题
项目上传部分
LeetCode简单题之重新排列日志文件
Detailed description of self feeling of auricular point weight loss 0422
Kubernetes in browser and IDE | interactive learning platform killercoda
Flink SQL实现流批一体
DOM学习笔记---遍历页面所有元素节点
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code