1.wait与notify实现
package com.learning.thread_order;
import java.util.concurrent.locks.*;
/**
* @Author wangyouhui
* @Description 线程交替执行
**/
public class ThreadInTurnExecute {
public static void main(String[] args) {
WaitNotify waitNotify = new WaitNotify(1, 5);
Thread thread1 = new Thread(()->{
waitNotify.print("线程1执行", 1, 2);
});
Thread thread2 = new Thread(()->{
waitNotify.print("线程2执行", 2, 3);
});
Thread thread3 = new Thread(()->{
waitNotify.print("线程3执行", 3, 1);
});
thread1.start();
thread2.start();
thread3.start();
}
}
class WaitNotify{
// 等待标记
private int flag;
// 循环次数
private int loopNumber;
// 构造方法
public WaitNotify(int flag, int loopNumber){
this.flag = flag;
this.loopNumber = loopNumber;
}
// 打印方法
public void print(String info, 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.println(info);
flag = nextFlag;
this.notifyAll();
}
}
}
}
2.await与notify实现
package com.learning.thread_order;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* @Author wangyouhui
* @Description 线程交替执行
**/
public class ThreadInTurnExecute2 {
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();
Thread thread1 = new Thread(()->{
awaitSignal.print("线程1执行", a, b);
});
Thread thread2 = new Thread(()->{
awaitSignal.print("线程2执行", b, c);
});
Thread thread3 = new Thread(()->{
awaitSignal.print("线程3执行", c, a);
});
thread1.start();
thread2.start();
thread3.start();
Thread.sleep(1000);
awaitSignal.lock();
try{
System.out.println("开始");
a.signal();
}finally {
awaitSignal.unlock();
}
}
}
class AwaitSignal extends ReentrantLock {
private int loopNumber;
public AwaitSignal(int loopNumber){
this.loopNumber = loopNumber;
}
public void print(String info, Condition current, Condition next) {
for(int i=0;i<loopNumber;i++){
lock();
try{
current.await();
System.out.println(info);
next.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
unlock();
}
}
}
}
3.park与unpark实现
package com.learning.thread_order;
import java.util.concurrent.locks.LockSupport;
/**
* @Author wangyouhui
* @Description 线程交替执行
**/
public class ThreadInTurnExecute3 {
static Thread thread1;
static Thread thread2;
static Thread thread3;
public static void main(String[] args) throws InterruptedException {
ParkUnpark parkUnpark = new ParkUnpark(5);
thread1 = new Thread(()->{
parkUnpark.print("线程1执行", thread2);
});
thread2 = new Thread(()->{
parkUnpark.print("线程2执行", thread3);
});
thread3 = new Thread(()->{
parkUnpark.print("线程3执行", thread1);
});
thread1.start();
thread2.start();
thread3.start();
LockSupport.unpark(thread1);
}
}
class ParkUnpark {
private int loopNumber;
public ParkUnpark(int loopNumber){
this.loopNumber = loopNumber;
}
public void print(String info, Thread next) {
for (int i = 0; i < loopNumber; i++) {
LockSupport.park();
System.out.println(info);
LockSupport.unpark(next);
}
}
}