2021年3月29日星期一

Java Thread Synchronization problem in same resource

I am trying to operate on the same source with two threads. I designed a typical producer and consumer problem for it. While setting the value in the resource class with the producer, I want to get setted values with the consumer one by one. The output I want should be like this:

Producer -> Setting data = 0  Consumer -> Getting data = 0  Producer -> Setting data = 1  Consumer -> Getting data = 1  Producer -> Setting data = 2  Consumer -> Getting data = 2  Producer -> Setting data = 3  Consumer -> Getting data = 3  Producer -> Setting data = 4  Consumer -> Getting data = 4  

Here is my Resource class:

public class Resource{            private int value;      private boolean current = false;      public synchronized void setValue(int val) {          while(current == true) {          try {              wait();          }catch(Exception ex) {}}          value = val;          current = true;          notifyAll();            }            public synchronized int getValue() {          while(current == false) {          try {              wait();          }catch(Exception ex) {}}                    current = false;          notifyAll();          return value;            }  }  

And main method and Producer,Consumer class is here:

    class Producer extends Thread{        private Resource rs;      public Producer(Resource rs1) {          rs = rs1;      }      public void run() {          for(int i = 0 ; i < 5 ; i++) {              rs.setValue(i);              System.out.println("Producer -> Setting data = " + i);              try {                  sleep(100);              }catch(Exception ex){                  ex.printStackTrace();              }          }                }        }    class Consumer extends Thread{            private Resource rs;      public Consumer(Resource rs1) {          rs = rs1;      }      public void run() {          int value = 0;          for(int i = 0 ; i < 5; i++) {              value = rs.getValue();              System.out.println("Consumer -> Getting data= " + i);              try {                  sleep(100);              }catch(Exception ex) {                  ex.printStackTrace();              }          }                          }        }    public class Dependent {            public static void main(String[] args) throws IOException {                    Resource res = new Resource();          Producer p1 = new Producer(res);          Consumer c1 = new Consumer(res);                p1.start();          c1.start();                }    }  

Although I use synchronized, wait and notifyAll keywords in the methods in the resource class, the threads continue to work without waiting for each other. Where am I making a mistake? I've seen a code sample similar to this code sample in a java book, there doesn't seem to be a problem.

When I write without adding the current boolean variable, the code doesn't even work. That's why I had to add it by looking from the book. Don't the threads need to work synchronously without checking the Current value?

https://stackoverflow.com/questions/66862790/java-thread-synchronization-problem-in-same-resource March 30, 2021 at 07:10AM

没有评论:

发表评论