【问题标题】:Java Thread Synchronization problem in same resource同一资源中的Java线程同步问题
【发布时间】:2021-03-29 23:10:07
【问题描述】:

我正在尝试使用两个线程对同一个源进行操作。我为它设计了一个典型的生产者和消费者问题。在与生产者一起设置资源类中的值时,我想与消费者一一获取设置值。我想要的输出应该是这样的:

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

这是我的资源类:

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;
    
    }
}

还有main方法和Producer、Consumer类在这里:

    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();
        
    }

}

虽然我在资源类中的方法中使用了synchronized、wait和notifyAll关键字,但是线程继续工作而不需要互相等待。我在哪里犯错?我在java书上看过类似这个code sample的code sample,好像没有问题。

当我在不添加当前布尔变量的情况下编写时,代码甚至不起作用。这就是为什么我必须通过从书中查找来添加它。不检查当前值,线程不需要同步工作吗?

【问题讨论】:

    标签: java multithreading synchronization producer-consumer


    【解决方案1】:

    他们确实互相等待,但是线程同步操作比 Thread.sleep(100) 快很多,很多,所以你看不出来。您的测试代码打印“i”而不是“值”,这是可疑的。在其中一个线程(例如,在消费者中)中删除Thread.sleep(100),您会发现消费者仍然需要大约半秒才能完成 - 因为它每次调用 @ 时都会等待大约 100 毫秒资源上的 987654322@,因为该调用将阻塞(卡在该 wait() 循环中),直到生产者调用 .setValue ,它大约每 100 毫秒执行一次。

    您的资源对象“有效”,对于“有效”的某些值,但设计非常糟糕,从核心库(例如 java.util.concurrent.Latch)重新创建已经存在和更好实现的类,并且忽略中断并将盲目地继续等待。

    它们的 API 的名称也有点奇怪,因为 get 调用具有相当大的副作用。 get 更像是 get and clear 操作:在一个 get 操作之后,另一个 get 操作将永远冻结线程,或者至少,直到某个线程设置一个值。

    【讨论】:

      【解决方案2】:

      你觉得怎么样?

      import java.io.IOException;
      
      class Resource {
      
          private volatile Integer value;
          public synchronized void setValue(int val) {
              while(value != null && !value.equals(val)) {
                  try {
                      wait();
                  }catch(Exception ex) {}}
              value = val;
              notifyAll();
      
          }
      
          public synchronized int getValue() {
              while(value == null) {
                  try {
                      wait();
                  }catch(Exception ex) {}}
      
              int answer = value;
              value = null;
              notifyAll();
              return answer;
      
          }
      }
      
      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() {
              for(int i = 0 ; i < 5; i++) {
                  int value = rs.getValue();
                  System.out.println("Consumer -> Getting data= " + value);
                  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();
      
          }
      
      }
      

      或

      class Resource {
          private static final int WAIT_VALUE = -1;
          private volatile int value = WAIT_VALUE;
          public synchronized void setValue(int val) {
              while(value > WAIT_VALUE && value != val) {
                  try {
                      wait();
                  }catch(Exception ex) {}}
              value = val;
              notifyAll();
      
          }
      
          public synchronized int getValue() {
              while(value == WAIT_VALUE) {
                  try {
                      wait();
                  }catch(Exception ex) {}}
      
              int answer = value;
              value = WAIT_VALUE;
              notifyAll();
              return answer;
      
          }
      }
      

      【讨论】:

      • 我都试过了,但仍然出现同样的问题。但是为什么我必须在 Resource 类的两个方法中都使用 while 循环。当我调用它们时,我希望它们能够毫无问题地启动。
      • 您要显示 0 0 1 1 2 2 还是 0 1 2?
      • 我重新设计了我的课程并解决了问题。感谢您的帮助!
      猜你喜欢
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 2018-06-01
      相关资源
      最近更新 更多