public class Privider_Comsumer {
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{
try {
while(true){
data.increment();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
new Thread(()->{
try {
while(true) {
data.decrement();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B").start();
}
}
class Data{
private int number = 0;
public synchronized void increment() throws InterruptedException {
while(number >= 10){
this.wait();
}
number++;
System.out.println(Thread.currentThread().getName()+ this.number);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
while(number < 1){
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName()+this.number);
this.notifyAll();
}
}