当前位置:网站首页>Two threads print odd and even numbers interactively
Two threads print odd and even numbers interactively
2022-04-23 07:41:00 【cqwoniu】
1、 Problem description
To print alternately two threads by program 0-100 Odd even number of
2、 Their thinking
Let thread 1 Wake up other threads after printing , Then get out of the lock , I go to sleep , Because it enters the sleep state, it will not compete with other threads for locks , There are only threads 2 Can get the lock , Threads 2 Execute... With the same logic , Wake up the thread 1 And give up the lock , I go to sleep .
Code
private int count = 0;
private final Object lock = new Object();
public void turning() throws InterruptedException {
Thread even = new Thread(() -> {
while (count <= 100) {
synchronized (lock) {
System.out.println(" even numbers : " + count++);
lock.notifyAll();
try {
// If it's not over , Then release the current lock and sleep
if (count <= 100) {
lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread odd = new Thread(() -> {
while (count <= 100) {
synchronized (lock) {
System.out.println(" Odd number : " + count++);
lock.notifyAll();
try {
// If it's not over , Then release the current lock and sleep
if (count <= 100) {
lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
even.start();
// Make sure that even threads get the lock first
Thread.sleep(1);
odd.start();
}
public class TurningRunner implements Runnable {
private int count=0;
private final Object lock = new Object();
@Override
public void run() {
while (count <= 100) {
// Get the lock
synchronized (lock) {
// Print when you get the lock
System.out.println(Thread.currentThread().getName() + ": " + count++);
// Wake up other threads
lock.notifyAll();
try {
if (count <= 100) {
// If the mission is not over , Then release the current lock and sleep
lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
版权声明
本文为[cqwoniu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230622031922.html
边栏推荐
猜你喜欢

Javscript gets the real suffix of the file
![[Educational Codeforces Round 80] 解题报告](/img/54/2fd298ddce3cd3e28a8fe42b3b8a42.png)
[Educational Codeforces Round 80] 解题报告

反思|开启B站少女心模式,探究APP换肤机制的设计与实现

Solutions to common problems in visualization (VII) solutions to drawing scale setting

数据分析入门 | kaggle泰坦尼克任务(四)—>数据清洗及特征处理

如何判断点是否在多边形内(包含复杂多边形或者多边形数量很多的情况)

SAP PI/PO rfc2Soap 发布rfc接口为ws示例

学习笔记5-梯度爆炸和梯度消失(K折交叉验证)

Take you to travel in space, and American photography technology provides comprehensive technical support for aerospace creative applet

H5 case development
随机推荐
What is a closure?
manjaro安装与配置(vscode,微信,美化,输入法)
Mysql 数据库从设计上的优化
数据分析入门 | kaggle泰坦尼克任务(四)—>数据清洗及特征处理
保洁阿姨都能看懂的中国剩余定理和扩展中国剩余定理
数论分块(整除分块)
anaconda3安装
Discussion on arrow function of ES6
ogldev-读书笔记
3.排序语句
技术小白的第一篇(表达自己)
javscript获取文件真实后缀名
对STL容器的理解
[Ted series] how to get along with inner critics?
Mysql的存储引擎
可视化常见问题解决方案(七)画图刻度设置解决方案
页面动态显示时间(升级版)
可视化之路(九)Arrow类详解
积性函数与迪利克雷卷积
5.SQL99标准:内连接和外连接