//打印abc五次
public static void main(String[] args) {
PrintTest p = new PrintTest(1,5);
new Thread(()->{
p.print(1,2,"a");
}).start();
new Thread(()->{
p.print(2,3,"b");
}).start();
new Thread(()->{
p.print(3,1,"c");
}).start();
}
static class PrintTest{
private int status;
private int lookNumber;
public void print(int waitStatus,int nextStatus,String str) {
for (int i =0;i<lookNumber;i++){
synchronized (this){
if(status != waitStatus){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.print(str);
status = nextStatus;
this.notifyAll();
}
}
}
public PrintTest(int status, int lookNumber) {
this.status = status;
this.lookNumber = lookNumber;
}
}
顺序打印5次abc
最新推荐文章于 2024-06-24 11:09:01 发布