package com.ping.concurrent;
//资源类
class Print{
//设置条件保证先打印数字
private boolean flag = true;
//这个是要打印数字的起始
private int number = 0;
public synchronized void printNumber() throws InterruptedException {
if (!flag){
this.wait();
}
for (int i = 0; i < 2 ; i++) {
System.out.print(++number+"\t");
}
flag = false;
this.notify();
}
public synchronized void printAZ(int i) throws InterruptedException {
if (flag){
this.wait();
}
System.out.print((char) ('A'+i)+"\t");
flag = true;
this.notify();
}
}
/**
* @Auther: ping.cheng.yu
* @Date: 2018/8/1/001 21:17
* @Description:
*/
public class NotifyWait {
public static void main(String[] args) {
Print print = new Print();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0 ;i < 26;i ++){
try {
print.printNumber();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0 ;i < 26;i ++){
try {
print.printAZ(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}