固定运行顺序
使用 wait notify
public class Test39 {
static Object obj = new Object();
static boolean t2runed = false;
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
synchronized (obj) {
while (!t2runed) {
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.print(1);
});
Thread t2 = new Thread(() -> {
System.out.print(2);
synchronized (obj) {
t2runed = true;
obj.notifyAll();
}
});
t1.start();
t2.start();
}
}
Park Unpark顺序输出
public class Test40 {
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
LockSupport.park();
System.out.println("1");
});
Thread t2 = new Thread(() -> {
System.out.println("2");
LockSupport.unpark(t1);
});
t1.start();
t2.start();
}
}
交替输出
使用wait notify交替输出
**
* wait notify交替输出
*/
public class Test41 {
private int falg;
private int printNumber;
public Test41(int falg, int printNumber) {
this.falg = falg;
this.printNumber = printNumber;
}
private void print(String string, int nowFalg, int nextFlag) throws InterruptedException {
for (int i = 0; i < printNumber; i++) {
synchronized (this) {
while (this.falg != nowFalg) {
this.wait();
}
System.out.print(string+",");
this.falg = nextFlag;
this.notifyAll();
}
}
}
public static void main(String[] args) {
Test41 syncWaitNotify = new Test41(1, 5);
new Thread(() -> {
try {
syncWaitNotify.print("a", 1, 2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
try {
syncWaitNotify.print("b", 2, 3);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
new Thread(() -> {
try {
syncWaitNotify.print("c", 3, 1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
运行结果
a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,
使用Lock 条件变量交替输出
public class Test42 extends ReentrantLock {
public static void main(String[] args) throws InterruptedException {
Test42 as = new Test42(5);
Condition aWaitSet = as.newCondition();
Condition bWaitSet = as.newCondition();
Condition cWaitSet = as.newCondition();
new Thread(() -> {
as.print("a", aWaitSet, bWaitSet);
}).start();
new Thread(() -> {
as.print("b", bWaitSet, cWaitSet);
}).start();
new Thread(() -> {
as.print("c", cWaitSet, aWaitSet);
}).start();
Thread.sleep(1000);
as.lock();
try {
aWaitSet.signal();
} finally {
as.unlock();
}
}
private int loopNumber;
public Test42(int loopNumber) {
this.loopNumber = loopNumber;
}
public void print(String str, Condition currentCondition, Condition nextCondition) {
for (int i = 0; i < loopNumber; i++) {
this.lock();
try {
currentCondition.await();
System.out.print(str + ",");
nextCondition.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
this.unlock();
}
}
}
}
运行结果
a,b,c,a,b,c,a,b,c,a,b,c,a,b,c,
使用park unpark 条件变量交替输出
public class Test43 extends ReentrantLock {
private static Thread t1;
private static Thread t2;
private static Thread t3;
public static void main(String[] args) throws InterruptedException {
SyncPark syncPark = new SyncPark(5);
t1 = new Thread(() -> {
syncPark.print("a", t2);
});
t2 = new Thread(() -> {
syncPark.print("b", t3);
});
t3 = new Thread(() -> {
syncPark.print("c", t1);
});
t1.start();
t2.start();
t3.start();
LockSupport.unpark(t1);
}
}
class SyncPark {
private int loopNumber;
public SyncPark(int loopNumber) {
this.loopNumber = loopNumber;
}
public void print(String str, Thread nextThread) {
for (int i = 0; i < loopNumber; i++) {
LockSupport.park();
System.out.print(str + ",");
LockSupport.unpark(nextThread);
}
}
}