当前位置:网站首页>Multiple forms of alternate printing implemented by multithreading
Multiple forms of alternate printing implemented by multithreading
2022-04-22 00:07:00 【Yoke______】
Multiple implementation forms of alternating printing realized by multithreading . To put it bluntly, it is to use java‘ Various synchronizers in realize the requirement of alternating printing .
sync How to implement
package com.AQS.JiaoTiDaYin;
/** * @Author:XK * @Date: Created in 12:00 2022/4/20 * @Description: Multiple implementation methods of multithreaded alternating printing (sync) **/
public class JiaotiDayinSync {
private static int counter=0;
private static Object object= new Object();
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (cnt<3){
synchronized (object){
if(counter%3!=0){
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
System.out.println("1");
cnt++;
counter++;
}
object.notifyAll();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (cnt<3){
synchronized (object){
if(counter%3!=1){
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
System.out.println("2");
cnt++;
counter++;
}
object.notifyAll();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (cnt<3){
synchronized (object){
if(counter%3!=2){
try {
object.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
System.out.println("3");
cnt++;
counter++;
}
object.notifyAll();
}
}
}
}).start();
}
}
lock class ReentrantLock How to implement
package com.AQS.JiaoTiDaYin;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/** * @Author:XK * @Date: Created in 12:10 2022/4/20 * @Description:lock Realization ReentrantLock Realization **/
public class Lock123 {
private static int state = 0;
private static Lock lock= new ReentrantLock();
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3;) {
lock.lock();
try {
while (state%3==0){
System.out.println(1);
state++;
i++;
}
}finally {
lock.unlock();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3;) {
lock.lock();
try {
while (state%3==1){
System.out.println(2);
state++;
i++;
}
}finally {
lock.unlock();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3;) {
lock.lock();
try {
while (state%3==2){
System.out.println(3);
state++;
i++;
}
}finally {
lock.unlock();
}
}
}
}).start();
}
}
semaphore Semaphore implementation
package com.AQS.JiaoTiDaYin;
import java.util.concurrent.Semaphore;
/** * @Author:XK * @Date: Created in 14:41 2022/4/20 * @Description: **/
public class Sema123 {
private static Semaphore A = new Semaphore(1);
private static Semaphore B = new Semaphore(0);
private static Semaphore C = new Semaphore(0);
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
try {
A.acquire();
System.out.println(1);
B.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
try {
B.acquire();
System.out.println(2);
C.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 3; i++) {
try {
C.acquire();
System.out.println(3);
A.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
版权声明
本文为[Yoke______]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220005128931.html
边栏推荐
- 08. 树莓派安装MySQL
- Kuangshi Research Institute | dark vision network: RGB IR Fusion low illumination imaging method using depth inconsistent a priori
- Sp4522b is a mobile power IC with integrated output for 2A charging and boosting
- A list of the latest changes in Mandelbrot set -- mandelbox, mandelbulb, burning ship, nebulabrot
- 稳压二极管限流电阻怎么选择
- 基于apache集合工具包的并集、交集、差集工具类
- 犯二的程度
- 续流二极管在开关电源的作用
- XSS-Game Level 4
- How to choose the current limiting resistor of Zener diode
猜你喜欢

细说MOS管知识-MOS管高端驱动与低端驱动解析和原理及区别

How to obtain a module of a certain layer in pytorch? (it is convenient to change gradient, obtain feature map, cam, etc.)

DetNet: A Backbone network for Object Detection

浏览器原理学习笔记1-浏览器进程

AVL balanced binary tree and its four rotation modes

Deploy node project to ECS detailed tutorial original

已拿offer,面试必备知识点

redis源码之链表(adlist.h和adlist.c)(篇一)

Deep learning (15): instructions for kitti2bag

继电器为什么要并联二极管
随机推荐
[transfer] differences and relations between collection set map vector list
B. Vlad and Candies
Gartner发布新兴技术研究:深入洞悉元宇宙
【读书笔记】《经验会计与财务研究方法-原理、应用及SAS实现》 鲁桂华
Analysis of EMI suppression methods of switches and diodes in switching power supply
Deep learning (15): instructions for kitti2bag
FreeFileSync的使用教程
2022年江西省安全员A证考试题及答案
Redis starts the service and connects the client
插入创建数组
刷题整理(一)
Low cost Wireless SoC chip for fs68001a and fs68003
How to make knowledge map assist multi round dialogue
浏览器原理学习笔记1-浏览器进程
C dual insurance process monitor lol ensures that the monitored program runs' almost 'forever Keywords: process operation process view create process
FANSEA 4W 单线圈发射无线充5W模块
09. Raspberry pie ASP Net environment configuration
14 custom XML
14 自定义xml
Gartner announces emerging technology research: insight into the meta universe