1.固定运行顺序
(1)wait-notify
public class Asyn {
//1,2
static Object obj=new Object();
static int flag=0;
public static void main(String[] args) throws InterruptedException {
Thread t1= new Thread(()->{
synchronized (obj) {
System.out.println("1");
flag=1;
obj.notifyAll();
}
},"t1");
Thread t2= new Thread(()->{
synchronized (obj){
while (flag!=1){
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag=0;
System.out.println("2");
}
},"t2");
t2.start();
t1.start();
}
(2)await-signal
@Test
public void awaitsingnal(){
ReentrantLock reentrantLock=new ReentrantLock();
Condition condition=reentrantLock.newCondition();
Thread t2=new Thread(()->{
try {
reentrantLock.lock();
while (flag!=1){
condition.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
reentrantLock.unlock();
}
System.out.println("2");
},"t2");
Thread t1= new Thread(()->{
try {
reentrantLock.lock();
System.out.println("1");
flag=1;
condition.signal();
}finally {
reentrantLock.unlock();
}
},"t1");
t1.start();
t2.start();
}
(3)part-unpart
public void testpart(){
Thread t2=new Thread(()->{
LockSupport.park();
System.out.println("2");
},"t2");
Thread t1= new Thread(()->{
System.out.println("1");
LockSupport.unpark(t2);
},"t1");
t2.start();
t1.start();
}
2.交替输出
public class Alternate {
static Thread t1,t2,t3;
//wait-notify
public static void main(String[] args) throws InterruptedException {
Lock lock=new Lock(3);
Thread A=new Thread(()->{
try {
lock.print("a",1,2);
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A");
Thread B=new Thread(()->{
try {
lock.print("b",2,3);
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B");
Thread C=new Thread(()->{
try {
lock.print("c",3,1);
} catch (InterruptedException e) {
e.printStackTrace();
}
},"C");
C.start();
B.start();
A.start();
}
//await-signal
@Test
public void testre() throws InterruptedException {
AwaitSignal awaitSignal=new AwaitSignal(3);
Condition A=awaitSignal.newCondition();
Condition B=awaitSignal.newCondition();
Condition C=awaitSignal.newCondition();
new Thread(()->{
awaitSignal.print("a",A,B);
}).start();
new Thread(()->{
awaitSignal.print("b",B,C);
}).start();
new Thread(()->{
awaitSignal.print("c",C,A);
}).start();
Thread.sleep(1000);
awaitSignal.lock();
try{
A.signal();
}finally {
awaitSignal.unlock();
}
}
//part-unpart
@Test
public void testPartUnpart() throws InterruptedException {
PartUnpart partUnpart=new PartUnpart(2);
t1=new Thread(()->{
partUnpart.print("a",t2);
});
t2=new Thread(()->{
partUnpart.print("b",t3);
});
t3=new Thread(()->{
partUnpart.print("c",t1);
});
t3.start();
t1.start();
t2.start();
Thread.sleep(500);
LockSupport.unpark(t1);
}
}
class PartUnpart{
private int LoopNumber;
public PartUnpart(int loopNumber) {
LoopNumber = loopNumber;
}
public void print(String str, Thread next){
for (int i = 0; i <LoopNumber ; i++) {
LockSupport.park();
System.out.print(str);
LockSupport.unpark(next);
}
}
}
class AwaitSignal extends ReentrantLock{
private int LoopNumber;
public AwaitSignal(int loopNumber) {
LoopNumber = loopNumber;
}
public void print(String str,Condition con,Condition next){
for (int i = 0; i < LoopNumber; i++) {
lock();
try {
con.await();
System.out.print(str);
next.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
unlock();
}
}
}
}
class Lock{
private int count=1;
private int loopNumber;
public Lock(int loopNumber) {
this.loopNumber = loopNumber;
}
public int getCount() {
return count;
}
public void print(String str,int now,int next) throws InterruptedException {
for (int i = 0; i <loopNumber ; i++) {
synchronized (this) {
while (count!=now){
wait();
}
System.out.print(str);
count=next;
notifyAll();
}
}
}
}