java - 使用 RentrantLock 实现生产者消费者时出现 IllegalMonitorStateException

标签 java multithreading

我试图在java中使用ReentrantLock实现生产者消费者

    Condition producerlock = lock.newCondition();
    Condition consumerlock = lock.newCondition();

它有两个条件,一个用于生产者,另一个用于消费者。

这里我们有一个处理器类,有两个方法生产者消费者和一个堆栈

   Stack<Integer> hellostrack = new Stack<>();



 public void produce() throws InterruptedException {
        lock.tryLock();
        System.out.println("inside producer method");
        while (true) {
            try {

                if (hellostrack.size() > 8) {
                    System.out.println("stack is full its time for me to go to sleep");
                    producerlock.await();
                }
                System.out.println("thread is alive and kicking");
                hellostrack.add(new Random().nextInt());
                consumerlock.signalAll();


            } finally {
                System.out.println("Exception occours in producer Thread");
                lock.unlock();
            }
        }
    }


public void consume() throws InterruptedException{
             System.out.println("inside consumer method");
             lock.tryLock();
          try {
              while (true) {
                  if (hellostrack.isEmpty()) {
                      System.out.println("stack is empty im going to sleep");
                      consumerlock.await();

                  } else {
                      System.out.println("poping elelmts from stock" + hellostrack.pop());
                      consumerlock.signalAll();


                  }

          } }finally {
              System.out.println("Exception occours at consumer");
              lock.unlock();
          }
     }

正如你所看到的,当堆栈达到一定限制时,生产者将进入休眠状态,当堆栈为空时,消费者也将进入休眠状态

但是当我在两个线程中运行它们时

Processor p  = new Processor();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    p.consume();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t12 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    p.produce();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();
        t12.start();



i get illegal state exception 


inside consumer method
stack is empty im going to sleep
inside producer method
thread is alive and kicking
Exception occours in producer Thread
thread is alive and kicking
Exception occours in producer Thread
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException
    at java.base/java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:149)
    at java.base/java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1302)
    at java.base/java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:439)
    at Processor.produce(Processor.java:30)
    at Processor$2.run(Processor.java:76)
    at java.base/java.lang.Thread.run(Thread.java:834)
poping elelmts from stock891164354
poping elelmts from stock-1958956829
stack is empty im going to sleep

最佳答案

除了 @JohnVint's answer ,您的代码还存在一些其他问题。

  1. 您正在使用Lock.tryLock() :

    Acquires the lock only if it is free at the time of invocation.

    Acquires the lock if it is available and returns immediately with the value true. If the lock is not available then this method will return immediately with the value false.

    A typical usage idiom for this method would be:

    Lock lock = ...;
    if (lock.tryLock()) {
      try {
         // manipulate protected state
      } finally {
        lock.unlock();
      }
    } else {
      // perform alternative actions
    }
    

    This usage ensures that the lock is unlocked if it was acquired, and doesn't try to unlock if the lock was not acquired.

    您的代码不会检查 tryLock 的结果,这意味着线程有机会在不持有锁的情况下进入 protected 代码。这意味着有可能在不持有锁的情况下调用 await()signalAll()unlock() - 除了不正确的情况之外同步访问。

    在这种情况下您要调用的方法是 Lock.lock() :

    Acquires the lock.

    If the lock is not available then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired.

    这意味着线程将等待,直到获得锁才继续前进。但是,由于您的方法已经抛出 InterruptedException,您不妨使用 Lock.lockInterruptibly() 。它与 lock() 基本相同,但等待可以被中断。

  2. 您正在 consume() 方法中调用 consumerlock.signalAll()

    一旦您消耗了一个元素,您想要通知生产者有更多可用空间。您应该调用 Producerlock.signalAll()。

  3. 您不能在循环内调用 await()

    最好在检查条件的循环内调用 await()。原因是线程可能因任何原因而被唤醒(很少)。如果发生这种情况,并且存在循环,线程将重新检查条件,如果合适,将再次调用 await()

    此外,您正在使用signalAll()。该方法通知所有等待线程醒来,尝试获取锁,然后继续。由于您不使用循环,所以唤醒的所有线程将简单地继续执行任何修改 - 可能会导致不一致/不正确的状态。相反,有循环意味着如果唤醒的线程之一导致等待条件再次为真,则任何后续线程都将返回等待状态。

    使用循环看起来像:

    while (hellostrack.size() > 8) { // should this be >= 8?
        producerlock.await();
    }
    
    // and
    
    while (hellostrack.isEmpty()) {
        consumerlock.await();
    }
    

关于java - 使用 RentrantLock 实现生产者消费者时出现 IllegalMonitorStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54897078/

相关文章:

java - Hibernate——将具有单个值的 View 映射到只读字段

java - SonarQube 5.1 为 java 添加自定义规则

java - 如何使 InputStreamReader 在编码无效数据时失败?

multithreading - 检测低用户事件并在后台检查电子邮件

android - 具有不同按钮 View 的多线程 : Android

java - Findbugs 说 "HTTP parameter directly written to HTTP header"

java - "center_horizontal|center_vertical"和 "center"有区别吗

c - 任务比较多的线程池

java - 多个线程是否应该从同一个 DataInputStream 读取数据?

c# - 如何等待表单加载,然后启动方法?