当前位置:网站首页>可重入锁线程等待唤醒示例
可重入锁线程等待唤醒示例
2022-04-22 05:33:00 【linsa_pursuer】
1.synchronized
package juc.one;
/**
* 第一步 创建资源类,定义属性和操作方法
* 第二步 判断 干活 通知
* 第三步 创建多个线程,调用资源类的操作方法
* 第四步 防止虚假唤醒问题
*/
//第一步 创建资源类,定义属性和操作方法
class Share {
//初始值
private int number = 0;
//+1的方法
public synchronized void incr() throws InterruptedException {
//第二步 判断 干活 通知
while (number != 0){ //判断number值是否是0,如果不是0,等待
this.wait();//防止虚假唤醒,要在循环里调用
}
//如果number值是0,就+1操作
number++;
System.out.println(Thread.currentThread().getName()+" :: "+number);
//通知其他线程
this.notifyAll();
}
//-1的方法
public synchronized void decr() throws InterruptedException {
//判断
while(number != 1){
this.wait();
}
//干活
number--;
System.out.println(Thread.currentThread().getName()+" :: "+number);
//通知其他线程
this.notifyAll();
}
}
public class ThreadDemo1 {
//第三步 创建多个线程,调用资源类的操作方法
public static void main(String[] args) {
Share share = new Share();
//创建线程
new Thread(()->{
for(int i = 1; i<=10; i++){
try {
share.incr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"AA").start();
new Thread(()->{
for(int i = 1; i<=10; i++){
try {
share.decr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"BB").start();
new Thread(()->{
for(int i = 1; i<=10; i++){
try {
share.incr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"CC").start();
new Thread(()->{
for(int i = 1; i<=10; i++){
try {
share.decr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"DD").start();
}
}
2.ReentrantLock
package juc.two;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Share {
private int number = 0;
//创建lock
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
//+1
public void incr() throws InterruptedException {
//上锁
lock.lock();
try {
//判断
while (number != 0){
condition.await();
}
//干活
number++;
System.out.println(Thread.currentThread().getName()+" :: "+number);
//通知
condition.signalAll();
}finally {
//解锁
lock.unlock();
}
}
//-1
public void decr() throws InterruptedException {
lock.lock();
try{
//判断
while (number != 1){
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName()+" :: "+number);
condition.signalAll();
}finally {
lock.unlock();
}
}
}
public class ThreadDemo2 {
public static void main(String[] args) {
Share share = new Share();
//创建线程
new Thread(()->{
for(int i = 1; i<=10; i++){
try {
share.incr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"AA").start();
new Thread(()->{
for(int i = 1; i<=10; i++){
try {
share.decr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"BB").start();
new Thread(()->{
for(int i = 1; i<=10; i++){
try {
share.incr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"CC").start();
new Thread(()->{
for(int i = 1; i<=10; i++){
try {
share.decr();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"DD").start();
}
}
版权声明
本文为[linsa_pursuer]所创,转载请带上原文链接,感谢
https://blog.csdn.net/linsa_pursuer/article/details/124310477
边栏推荐
- Idea 2021.1 Useful settings
- Delete Xunlei and see the uninstall residue in the folder right-click menu
- Kaggle_NBME NLP比赛Baseline详解(2)
- 剑指 Offer 22. 链表中倒数第k个节点
- GBase 8s V8. 8 SQL Guide: tutorial-5.3
- The MySQL table removes duplicate values and keeps only one
- GBase 8s V8.8 SQL 指南:教程-6.1.1(4)
- Unity about the real machine failure of ispointerovergameobject interface
- MySQL事务
- GBase 8s V8. 8 SQL Guide: Tutorial - 6.1.2 (1)
猜你喜欢

Dolphin DB vscode plug-in tutorial

Use render texture to display 3D model animation on the UI

MySQL index

MySQL表的增删改查

力扣237. 删除链表指定节点

Cookie injection

【FedMD,一种利用模型蒸馏的异构FL训练方法】FedMD: Heterogenous Federated Learning via Model Distillation

再见2020,2021我来了

Application and selection of it power distribution and fire current limiting protector

使用easyexcel导出excel表格
随机推荐
AWD platform construction – cardinal
The MySQL table removes duplicate values and keeps only one
力扣237. 删除链表指定节点
Apache poi HSSF operation Excel
The unreal engine uses loadclass to load the blueprint class
【FedMD,一种利用模型蒸馏的异构FL训练方法】FedMD: Heterogenous Federated Learning via Model Distillation
Unsafe row of spark
遇见DOM
mysql中on duplicate key update 使用详解
AssetBundle packaging based on unitygameframework framework
I/O基础知识入门
MySQL transaction
MySQL JDBC programming
Write a program to automatically generate the two-color ball number of welfare lottery with MATLAB
xxxx(动态库名称): cannot open shared object file: No such file or directory
剑指 Offer 22. 链表中倒数第k个节点
GBase 8s V8. 8 SQL Guide: tutorial-5.3
Fundamentals of graphics - flood
MySQL 第7章 对数据表的复杂查询
Shenzhen Xishuangbanna