顺序输出
wait && notify
static boolean t2Runed = false;
static final Object o = new Object();
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
synchronized (o) {
while (!t2Runed) {
try {
o.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("t1");
}
});
Thread t2 = new Thread(() -> {
synchronized (o) {
System.out.println("t2");
t2Runed = true;
o.notify();
}
});
t1.start();
t2.start();
}
park && unpark
public static void main(String[] args) {
Thread t1 = new Thread(() -> {
LockSupport.park();
System.out.println("t1");
});
Thread t2 = new Thread(() -> {
System.out.println("t2");
LockSupport.unpark(t1);
});
t1.start();
t2.start();
}
交替输出 abcabcabcabcabc
ReentrantLock
public class SOutPutLock {
public static void main(String[] args) {
AwaitSigan as = new AwaitSigan(5);
Condition aC = as.newCondition();
Condition bC = as.newCondition();
Condition cC = as.newCondition();
new Thread(() -> {
as.print(aC, bC, "a");
}).start();
new Thread(() -> {
as.print(bC, cC, "b");
}).start();
new Thread(() -> {
as.print(cC, aC, "c");
}).start();
as.start(aC);
}
}
class AwaitSigan extends ReentrantLock {
public int loopNumber;
public AwaitSigan(int loopNumber) {
this.loopNumber = loopNumber;
}
public void start(Condition condition) {
this.lock();
try {
System.out.println("start");
condition.signal();
}finally {
this.unlock();
}
}
public void print(Condition cur, Condition next, String string) {
for (int i = 0; i < loopNumber; i++) {
this.lock();
try {
cur.await();
System.out.print(string);
next.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
this.unlock();
}
}
}
}
Park && Unpark
public class SOutPutParkAndUnPark {
public static void main(String[] args) {
SyncPark syncPark = new SyncPark(5);
Thread t1 = new Thread(() -> {
syncPark.print("a");
});
Thread t2 = new Thread(() -> {
syncPark.print("b");
});
Thread t3 = new Thread(() -> {
syncPark.print("c\n");
});
syncPark.setThreads(t1, t2, t3);
syncPark.start();
}
}
class SyncPark {
private int loopNumber;
private Thread[] threads;
public SyncPark(int loopNumber) {
this.loopNumber = loopNumber;
}
public void setThreads(Thread... threads) {
this.threads = threads;
}
public void print(String str) {
for (int i = 0; i < loopNumber; i++) {
LockSupport.park();
System.out.print(str);
LockSupport.unpark(nextThread());
}
}
private Thread nextThread() {
Thread current = Thread.currentThread();
int index = 0;
for (int i = 0; i < threads.length; i++) {
if(threads[i] == current) {
index = i;
break;
}
}
if(index < threads.length - 1) {
return threads[index+1];
} else {
return threads[0];
}
}
public void start() {
for (Thread thread : threads) {
thread.start();
}
LockSupport.unpark(threads[0]);
}
}
wait && notify
public class SOutPutWaitAndNotify {
public static void main(String[] args) {
SyncWaitNotify sy = new SyncWaitNotify(1, 5);
new Thread(()->{
sy.print(1, 2, "a");
}).start();
new Thread(()->{
sy.print(2, 3, "b");
}).start();
new Thread(()->{
sy.print(3, 1, "c");
}).start();
}
}
class SyncWaitNotify{
public int flag;
public int loopNumber;
public SyncWaitNotify(int flag, int loopNumber) {
this.flag = flag;
this.loopNumber = loopNumber;
}
public void print(int waitFlag, int nextFlag,String str){
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();
}
}
}
}