1、固定运行顺序
例如:两个线程,运行是必须先2后1打印。
1.1、Object之wait、notify版
在同步代码块中,wait开始后,CPU将释放给另一个线程使用,直到
①若wait(xxxx),则x秒后当前线程被唤醒,继续占用CPU。
②若wait(),则在使用notify(),后被唤醒当前线程。
/**
* 固定运行顺序: 先打印2,后打印1
*/
@Slf4j(topic = "test.FixedRunningOrderThread")
public class FixedRunningOrderThread {
private static final Object lock = new Object();
private static boolean t2Runned = false;// 表示t2是否运行过
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
synchronized (lock) {
while (!t2Runned) {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.debug("1");
}
},"t1");
Thread t2 = new Thread(() -> {
synchronized (lock) {
log.debug("2");
t2Runned = true;
lock.notify();
}
},"t2");
t1.start();
t2.start();
}
}
1.2、ReentrantLock之await、signal版
await、signal前需要获得锁。
await执行后,会释放锁,进入conditionObject等待。
await的线程被唤醒(或打断、或超时),重新竞争锁。
竞争锁成功后,从await后继续执行。
/**
* 固定运行顺序: 先打印2,后打印1
*/
@Slf4j(topic = "test.FixedRunningOrderThread2")
public class FixedRunningOrderThread2 {
private static final ReentrantLock lock = new ReentrantLock();
private static final Condition waitConditionSet = lock.newCondition();
private static boolean t2Runned = false;// 表示t2是否运行过
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
lock.lock();
try {
while (!t2Runned) {
try {
waitConditionSet.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
log.debug("1");
} finally {
lock.unlock();
}
},"t1");
Thread t2 = new Thread(() -> {
lock.lock();
try {
log.debug("2");
t2Runned = true;
waitConditionSet.signal();
} finally {
lock.unlock();
}
},"t2");
t1.start();
t2.start();
}
}
1.3、LockSupport之park、unpark版
// 暂停当前线程,暂停后,线程状态变为 WAITING
LockSupport.park();
// 恢复某个线程的运行,恢复后线程状态变为RUNNING
LockSupport.unpark(要恢复的线程对象);
/**
* 固定运行顺序: 先打印2,后打印1
*
* LockSupport.park(); 暂停当前线程,暂停后,线程状态变为 WAITING
* LockSupport.unpark(要恢复的线程对象); 恢复某个线程的运行,恢复后线程状态变为RUNNING
*/
@Slf4j(topic = "test.FixedRunningOrderThread3")
public class FixedRunningOrderThread3 {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
LockSupport.park();
log.debug("1");
},"t1");
Thread t2 = new Thread(() -> {
log.debug("2");
LockSupport.unpark(t1);
},"t2");
t1.start();
t2.start();
}
}
2、交替输出
示例:线程1输出a,线程2输出b,线程3输出c,交替输出5次。
2.1、Object之wait、notify版
/**
* 交替输出: Object之wait、notify版
* 输出内容:abcabcabcabcabc
*/
@Slf4j(topic = "test.FixedRunningOrderThread4")
public class FixedRunningOrderThread4 {
public static void main(String[] args) {
WaitNotify waitNotify = new WaitNotify(1,5);
new Thread(() -> {waitNotify.print("a",1,2);}).start();
new Thread(() -> {waitNotify.print("b",2,3);}).start();
new Thread(() -> {waitNotify.print("c",3,1);}).start();
}
}
@Slf4j(topic = "text.WaitNotify")
class WaitNotify {
private int flag; // 等待标记
private int loopNumber; // 循环次数
public WaitNotify(int flag, int loopNumber) {
this.flag = flag;
this.loopNumber = loopNumber;
}
/**
* @param str 打印内容
* @param waitFlag 等待标记
* @param nextFlag 下一个标记
*
* 线程 打印内容 标记 下个标记
* a a 1 2
* b b 2 3
* c c 3 1
*/
public void print(String str, int waitFlag, int nextFlag) {
for (int i = 0; i < loopNumber; i++) {
synchronized (this) {
// 不是需要打印的标记,进入waitset
while (flag != waitFlag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 是需要打印的标记,直接打印
System.out.print(str);
// 将打印标识切换为下一个
flag = nextFlag;
// 唤醒waitset中的所有线程
this.notifyAll();
}
}
}
}
2.2、ReentrantLock之await、signal版
/**
* 交替输出: ReentrantLock之await、signal版
*
* 输出内容:abcabcabcabcabc
*
*/
@Slf4j(topic = "test.FixedRunningOrderThread5")
public class FixedRunningOrderThread5 {
public static void main(String[] args) throws InterruptedException {
AwaitSignal awaitSignal = new AwaitSignal(5);
// 创建多个等待队列
Condition conditionA = awaitSignal.newCondition();
Condition conditionB = awaitSignal.newCondition();
Condition conditionC = awaitSignal.newCondition();
new Thread(() -> {awaitSignal.print("a",conditionA,conditionB);}).start();
new Thread(() -> {awaitSignal.print("b",conditionB,conditionC);}).start();
new Thread(() -> {awaitSignal.print("c",conditionC,conditionA);}).start();
// 启动前,先睡眠1s,因为需等待线程a、b、c都进入等待队列;
// 否则可能等待队列中还没有线程,无法唤醒,直接结束了,无法打印出内容
Thread.sleep(1000);
awaitSignal.lock();// 获取锁
try {
System.out.println("开始打印。。。");
// 先唤醒conditionA等待队列中的线程, 需先获得锁,才能调用signal
conditionA.signal();
} finally {
awaitSignal.unlock();// 释放锁
}
}
}
/**
* await前需要获得锁;
* await执行后,会释放锁,进入waitConditionSet等待。
* await的线程被唤醒(或打断、或超时),重新竞争锁; 竞争锁成功后,从await后继续执行。
*/
@Slf4j(topic = "text.AwaitSignal")
class AwaitSignal extends ReentrantLock {
private int loopNumber; // 循环次数
public AwaitSignal(int loopNumber) {
this.loopNumber = loopNumber;
}
/**
* @param str 打印内容
* @param currentCondition 当前的等待队列
* @param nextCondition 下一个等待队列
*/
public void print(String str, Condition currentCondition, Condition nextCondition) {
for (int i = 0; i < loopNumber; i++) {
lock(); // 获取锁
try {
try {
currentCondition.await(); // 当前线程进入等待
System.out.print(str); // 打印内容
nextCondition.signal(); // 唤醒下一个等待队列中的一个线程(先进先出)
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
unlock();// 释放锁
}
}
}
}
2.3、LockSupport之park、unpark版
/**
* 交替输出: LockSupport之park、unpark版
* 输出内容:abcabcabcabcabc
*/
@Slf4j(topic = "test.FixedRunningOrderThread6")
public class FixedRunningOrderThread6 {
private static Thread threadA;
private static Thread threadB;
private static Thread threadC;
public static void main(String[] args) throws InterruptedException {
ParkUnpark parkUnpark = new ParkUnpark(5);
threadA = new Thread(() -> {parkUnpark.print("a",threadB);});
threadB = new Thread(() -> {parkUnpark.print("b",threadC);});
threadC = new Thread(() -> {parkUnpark.print("c",threadA);});
threadA.start();
threadB.start();
threadC.start();
System.out.println("开始打印。。。");
LockSupport.unpark(threadA);
}
}
/**
* LockSupport.park(); 暂停当前线程,暂停后,线程状态变为 WAITING
* LockSupport.unpark(要恢复的线程对象); 恢复某个线程的运行,恢复后线程状态变为RUNNING
*/
@Slf4j(topic = "text.ParkUnpark")
class ParkUnpark {
private int loopNumber; // 循环次数
public ParkUnpark(int loopNumber) {
this.loopNumber = loopNumber;
}
/**
* @param str 打印内容
* @param nextThread 下一个需要唤醒的线程
*/
public void print(String str,Thread nextThread) {
for (int i = 0; i < loopNumber; i++) {
LockSupport.park(); // 暂停当前线程
System.out.print(str);
LockSupport.unpark(nextThread); // 唤醒下一个线程
}
}
}