I implemented producer consumer like this.
But it is throwing error.
I tried to use this method of using lock. Link
class Testclass {
Boolean isFresh = false;
int count = 0;
public synchronized void GET(String threadName){
while(!isFresh){
try {
isFresh.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("GET method was called : " + count + " " + threadName);
isFresh = false;
isFresh.notify();
}
public synchronized void PUT(String threadName){
while(isFresh){
try{
isFresh.wait();
}catch( InterruptedException e){
e.printStackTrace();
}
}
count++;
System.out.println("PUT method was called : " + count + " " + threadName);
isFresh = true;
isFresh.notify();
}
}
class Producer implements Runnable{
Testclass q;
String name;
Producer(Testclass q, String name){
this.q = q;
this.name = name;
}
public void run(){
while(true){
int time = (int)(Math.random() * 10000);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
q.PUT(this.name);
}
}
}
class Consumer implements Runnable{
Testclass q;
String name ;
Consumer(Testclass q,String name){
this.q = q;
this.name = name;
}
public void run(){
while(true){
int time = (int)(Math.random() * 10000);
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
q.GET(this.name);
}
}
}
public class Main {
public static void main(String[] args){
Testclass t = new Testclass();
Thread t1 = new Thread(new Consumer(t, "consumer 1"));
Thread t2 = new Thread(new Consumer(t, "consumer 2"));
Thread t3 = new Thread(new Producer(t, "producer 1"));
Thread t4 = new Thread(new Producer(t, "producer 2"));
t1.start();
t2.start();
t3.start();
t4.start();
try{
t1.join();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
this implementation throws following error.
Please explain why all the threads are throwing Illegal MonitorStateException?
PUT method was called : 1 producer 1
Exception in thread "Thread-2" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.notify(Native Method)
at Testclass.PUT(Main.java:34)
at Producer.run(Main.java:54)
at java.base/java.lang.Thread.run(Thread.java:832)
GET method was called : 1 consumer 1
Exception in thread "Thread-0" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.notify(Native Method)
at Testclass.GET(Main.java:19)
at Consumer.run(Main.java:76)
at java.base/java.lang.Thread.run(Thread.java:832)
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.wait(Native Method)
at java.base/java.lang.Object.wait(Object.java:321)
at Testclass.GET(Main.java:12)
at Consumer.run(Main.java:76)
at java.base/java.lang.Thread.run(Thread.java:832)
PUT method was called : 2 producer 2
Exception in thread "Thread-3" java.lang.IllegalMonitorStateException: current thread is not owner
at java.base/java.lang.Object.notify(Native Method)
at Testclass.PUT(Main.java:34)
at Producer.run(Main.java:54)
at java.base/java.lang.Thread.run(Thread.java:832)
Process finished with exit code 0
I want to know why is the output like that? and what is the correct way of implementing it?