线程固定顺序交替输出的三种方式(趁室友不在寒假在家偷偷学)

云惠网小编 2022年1月16日19:20:18
评论
2628字阅读8分45秒
摘要

目录synchronized方式(ReentrantLock)await & signal方式(LockSupport)park&unpark方式synchronized方式package com.atguigu.juc;public class ChangePrint { public static void main(String[] args) { WaitChange waitChange = new WaitChange(1, ..

广告也精彩

(ReentrantLock)await & signal方式


 synchronized方式

(ReentrantLock)await & signal方式

package com.atguigu.juc;
public class ChangePrint {
public static void main(String[] args) {
WaitChange waitChange = new WaitChange(1, 5);
new Thread(()->{
waitChange.print("a",1,2);
}).start();
new Thread(()->{
waitChange.print("b",2,3);
}).start();
new Thread(()->{
waitChange.print("c",3,1);
}).start();
}
}
class WaitChange {
private int flag;
private int loopNumber;
public WaitChange(int flag, int loopNumber) {
this.flag = flag;
this.loopNumber = loopNumber;
}
public void print(String str,int waitFlag,int nextFlag){
for (int i = 0; i < loopNumber; i++) {
synchronized (this){
while(flag != waitFlag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(str+" ");
flag = nextFlag;
this.notifyAll();
}
}
}
}
package com.atguigu.juc;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class ChangePrintReentrantLock {
public static void main(String[] args) throws InterruptedException {
ChangeReentrantLock reentrantLock = new ChangeReentrantLock(5);
Condition c1 = reentrantLock.newCondition();
Condition c2 = reentrantLock.newCondition();
Condition c3 = reentrantLock.newCondition();
new Thread(()->{
reentrantLock.print("a",c1,c2);
}).start();
new Thread(()->{
reentrantLock.print("b",c2,c3);
}).start();
new Thread(()->{
reentrantLock.print("c",c3,c1);
}).start();
Thread.sleep(1000);
reentrantLock.lock();
try {
System.out.println("开始");
c1.signal();
} finally {
reentrantLock.unlock();
}
}
}
class ChangeReentrantLock extends ReentrantLock {
private int loopNumber;
public ChangeReentrantLock(int loopNumber) {
this.loopNumber = loopNumber;
}
public void print(String str, Condition curCondition,Condition nextCondition){
for (int i = 0; i < loopNumber; i++) {
lock();
try {
try {
curCondition.await();
System.out.println(str);
nextCondition.signal();
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
unlock();
}
}
}
}

(LockSupport)park&unpark方式

package com.atguigu.juc;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
public class ChangePrintLockSupport {
static Thread t1;
static Thread t2;
static Thread t3;
public static void main(String[] args) {
ParkUnpark parkUnpark = new ParkUnpark(5);
t1 = new Thread(()->{
parkUnpark.print("a",t2);
});
t2 = new Thread(()->{
parkUnpark.print("b",t3);
});
t3 = new Thread(()->{
parkUnpark.print("c",t1);
});
t1.start();
t2.start();
t3.start();
LockSupport.unpark(t1);
}
}
class ParkUnpark {
private int loopNumber;
public ParkUnpark(int loopNumber) {
this.loopNumber = loopNumber;
}
public void print(String cur,Thread next){
for (int i = 0; i < loopNumber; i++) {
LockSupport.park();
System.out.println(cur);
LockSupport.unpark(next);
}
}
}

(LockSupport)park&unpark方式

目录

 synchronized方式

本文转自 https://blog.csdn.net/m0_60264772/article/details/122501668

腾讯云618
云惠网小编
SpringCloud -- Config、Bus解析 java

SpringCloud — Config、Bus解析

1、Config1.1、概述简介1. 分布式面临的问题微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要...
Java数据结构-了解复杂度 java

Java数据结构-了解复杂度

2.实例分析与计算  四.写在最后  // 计算斐波那契递归fibonacci的时间复杂度 int fibonacci(int N) { return N < 2 ? N : fibonacci...
Java数据结构-认识顺序表 java

Java数据结构-认识顺序表

目录二.顺序表1.概念及结构2.顺序表的实现打印顺序表获取顺序表的有效长度在pos位置新增元素判断是否包含某个元素查找某个元素对应的位置获取/查找pos位置的元素给pos位置的元素...
腾讯云618

发表评论