同步模式之顺序控制
同步模式之顺序控制
固定运行顺序
比如,必须先打印1再打印2
wait¬ify版
代码实现
@Slf4j(topic = "c.ControllerStepDemo")
public class ControllerStepDemo {
static final Object lock = new Object();
static boolean t2Runed = false;
public static void main(String[] args) {
Thread t1 = new Thread(()->{
synchronized (lock){
while (!t2Runed) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.debug("1");
}
},"t1");
Thread t2 = new Thread(()->{
synchronized (lock){
log.debug("2");
t2Runed = true;
lock.notify();
}
},"t2");
t1.start();
t2.start();
}
}
park&unpark
代码实现
public static void main(String[] args) {
Thread t1 = new Thread(()->{
LockSupport.park();
log.debug("1");
},"t1");
t1.start();
new Thread(()->{
log.debug("2");
LockSupport.unpark(t1);
},"t2").start();
}
交替输出
线程1输出a5次,线程2输出b5次,线程3输出c5次。要求输出结果为abcabcabcabcabc
wait¬ify
public class ControllerStepDemo2 {
public static void main(String[] args) {
WaitNotify waitNotify = new WaitNotify(1, 5);
new Thread(()->{
waitNotify.print("a",1,2);
},"t1").start();
new Thread(()->{
waitNotify.print("b",2,3);
},"t2").start();
new Thread(()->{
waitNotify.print("c",3,1);
},"t3").start();
}
}
class WaitNotify{
//打印
public void print(String str,int waitFlage,int nextFlage){
for (int i = 0; i < loopNumber; i++) {
synchronized (this){
while (flage!=waitFlage){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(str);
flage = nextFlage;
this.notifyAll();
}
}
}
//等待标记
private int flage;
//循环次数
private int loopNumber;
public WaitNotify(int flage, int loopNumber) {
this.flage = flage;
this.loopNumber = loopNumber;
}
}
await&sign
public class ControllerStepDemo2 {
public static void main(String[] args) throws InterruptedException {
AwaitSignal awaitSignal = new AwaitSignal(5);
Condition a = awaitSignal.newCondition();
Condition b = awaitSignal.newCondition();
Condition c = awaitSignal.newCondition();
new Thread(()->{
awaitSignal.print("a",a,b);
},"t1").start();
new Thread(()->{
awaitSignal.print("b",b,c);
},"t2").start();
new Thread(()->{
awaitSignal.print("c",c,a);
},"t3").start();
Thread.sleep(1000);
awaitSignal.lock();
try {
a.signal();
}finally {
awaitSignal.unlock();
}
}
}
class AwaitSignal extends ReentrantLock {
private int loopNumber;
public AwaitSignal(int loopNumber){
this.loopNumber = loopNumber;
}
public void print(String str,Condition current,Condition next){
for (int i = 0; i < loopNumber; i++) {
lock();
try {
current.await();
System.out.print(str);
next.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
unlock();
}
}
}
}
park&unpark
public class ControllerStepDemo2 {
static Thread t1;
static Thread t2;
static Thread t3;
public static void main(String[] args) throws InterruptedException {
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();
Thread.sleep(1000);
LockSupport.unpark(t1);
}
}
class ParkUnpark{
public void print(String str,Thread next){
for (int i = 0; i < 5; i++) {
LockSupport.park();
System.out.print(str);
LockSupport.unpark(next);
}
}
private int loopNumber;
public ParkUnpark(int loopNumber){
this.loopNumber = loopNumber;
}
}
原文地址:https://blog.csdn.net/m0_75113406/article/details/143518507
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!