0

我已經使用ReentrantLock和條件實施了生產者 - 消費者程序。如果我首先啓動生產者線程,我的實現運行沒有任何錯誤。但是,如果我首先啓動Consumer線程,則會收到IllegalMonitorStateException。請指出我的程序有什麼問題。生產者 - 消費者使用鎖和條件問題

這是我的實現。

public class ProducerConsumerReentrantLock { 
    public static void main(String[] args) throws InterruptedException { 
     List<Integer> list = new ArrayList<Integer>(10); 
     ReentrantLock lock = new ReentrantLock(); 
     Condition condition = lock.newCondition(); 
     int limit=10; 

     ProductionTaskReentrantLock produce = new ProductionTaskReentrantLock(lock, condition, limit, list); 
     ConsumptionTaskReentrantLock consume = new ConsumptionTaskReentrantLock(lock, condition, limit, list); 

     Thread productionWorker = new Thread(produce,"Producer"); 
     Thread consumptionWorker = new Thread(consume,"Consumer"); 

     consumptionWorker.start(); 
     productionWorker.start(); 
//  consumptionWorker.start(); 

     productionWorker.join(); 
     consumptionWorker.join(); 
    } 
} 

class ProductionTaskReentrantLock implements Runnable{ 

    List<Integer> list = null; 
    ReentrantLock lock; 
    Condition condition; 
    int limit; 

    public ProductionTaskReentrantLock(ReentrantLock lock, Condition condition, int limit, List<Integer> list) { 
     super(); 
     this.lock = lock; 
     this.condition = condition; 
     this.limit = limit; 
     this.list = list; 
    } 

    @Override 
    public void run() { 
     lock.lock(); 
     try{ 
      for (int i = 0; i < 10 ; i++) { 
       while(list.size()==limit){ 
        try { 
         System.out.println("List is full"); 
         condition.wait(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
       System.out.println("Produced "+i); 
       list.add(i); 
       System.out.println(list); 
       condition.signalAll(); 
       try { 
        Thread.sleep(500); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } finally { 
      lock.unlock(); 
     } 
    } 
} 

class ConsumptionTaskReentrantLock implements Runnable{ 

    List<Integer> list = null; 
    ReentrantLock lock; 
    Condition condition; 
    int limit; 

    public ConsumptionTaskReentrantLock(ReentrantLock lock, Condition condition, int limit, List<Integer> list) { 
     super(); 
     this.lock = lock; 
     this.condition = condition; 
     this.limit = limit; 
     this.list = list; 
    } 

    @Override 
    public void run() { 
     lock.lock(); 
     try{ 
      for (int i = 0; i < 10 ; i++) { 
       while(list.isEmpty()){ 
        try { 
         System.out.println("List is empty"); 
         condition.wait(); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
       System.out.println("Consumed "+list.remove(0)); 
       System.out.println(list); 
       condition.signalAll(); 
      } 
     } finally { 
      lock.unlock(); 
     } 
    } 

} 
+0

拋出:IllegalMonitorStateException - 投擲到。表明一個線程試圖等待一個對象的監視器或通知其他線程等待對象的監視器而不擁有指定的監視器。 – mcraen

+0

我不太瞭解Java線程以快速找出錯誤,但這是一個相當具有描述性的錯誤消息 – mcraen

+2

您可以嘗試使用condition.await()而不是等待並查看是否可以解決此問題? – Kamal

回答

0

查看類似下面的例子,你應該使用等待,而不是等待並使用的ReentrantLock,你已經在做返回的條件(見ReeentrantLock的Java DOC):

package reentrant_prodcons; 

import java.util.LinkedList; 
import java.util.Queue; 
import java.util.concurrent.locks.Condition; 
import java.util.concurrent.locks.ReentrantLock; 
import java.util.logging.Level; 
import java.util.logging.Logger; 


public class Reentrant_ProdCons { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 

     Queue<Integer> queue=new LinkedList<Integer>(); 
     ReentrantLock lock=new ReentrantLock(); 
     Condition con=lock.newCondition(); 
     final int size = 5; 

     new Producer(lock, con, queue, size).start(); 
     new Consumer(lock, con, queue).start(); 

    } 

} 


class Producer extends Thread{ 

    ReentrantLock lock; 
    Condition con; 
    Queue<Integer> queue; 
    int size; 

    public Producer(ReentrantLock lock, Condition con, Queue<Integer> queue, int size) { 
     this.lock = lock; 
     this.con = con; 
     this.queue = queue; 
     this.size=size; 
    } 


    public void run(){ 
     for(int i=0;i<10;i++){ 
      lock.lock(); 
      while(queue.size()==size){ 
       try { 
        con.await(); 
       } catch (InterruptedException ex) { 
        Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
      queue.add(i); 
      System.out.println("Produced : "+i); 
      con.signal(); 
      lock.unlock(); 
     } 
    } 

} 

class Consumer extends Thread{ 


    ReentrantLock lock; 
    Condition con; 
    Queue<Integer> queue; 


    public Consumer(ReentrantLock lock, Condition con, Queue<Integer> queue) { 
     this.lock = lock; 
     this.con = con; 
     this.queue = queue; 
    } 

    public void run(){ 
     for(int i=0;i<10;i++){ 
      lock.lock(); 
      while(queue.size()<1){ 
       try { 
        con.await(); 
       } catch (InterruptedException ex) { 
        Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
      System.out.println("Consumed : "+queue.remove()); 
      con.signal(); 
      lock.unlock(); 
     } 
    } 
}