生产者消费者使用java线程的问题

时间:2014-08-17 10:42:25

标签: java multithreading wait producer-consumer notify

我尝试编写生产者消费者问题。我所采用的问题陈述是我们有一个大小为10的ArrayList,生产者填充它直到它已满并且消费者显示它并使用它为空我正在使用wait()notify()机制。我到目前为止所做的代码是

Main.java

import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        List<Integer> buffer = new ArrayList<>();
        new Producer("Producer",buffer);
        new Consumer("Consumer",buffer);
    }

}

Producer.java

import java.util.ArrayList;
import java.util.List;

public class Producer extends Thread{
    String name;
    List<Integer> buffer=null;
    public Producer(String name,List<Integer> buffer) {
        this.setName(name);
        this.buffer=buffer;
        this.start();
        System.out.println("Inside Producer Constructor");
    }

    public void run(){
        synchronized(this){
            while(buffer.size()==10){
                try{
                    System.out.println("Put Consumer to wait");
                    wait();
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
        for(int i=0;i<10;i++){
            System.out.println("Added Elemtns to arraylist"+i);
            buffer.add(i);
        }
        System.out.println("Notifying All");
        notifyAll();            
    }

}

Consumer.java

import java.util.ArrayList;
import java.util.List;

public class Consumer extends Thread{
    List<Integer> buffer=null;
    public Consumer(String name,List<Integer> buffer){
        this.setName(name);
        this.buffer=buffer;
        this.start();
    }

    public void run(){
        synchronized(this){
            while(buffer.size()!=10){
                try{
                    System.out.println("Put Producer to wait");
                    wait();
                }catch(InterruptedException ie){
                    ie.printStackTrace();
                }
            }
            for(Integer i:buffer){
                System.out.println("Display from producer"+i);
            }
            buffer.clear();
            notifyAll();
        }
    }

}

但是我得到了IllegalMonitorStateException。其中一个游戏的O / P是

   Inside Producer Constructor
Added Elemtns to arraylist0
Added Elemtns to arraylist1
Added Elemtns to arraylist2
Added Elemtns to arraylist3
Added Elemtns to arraylist4
Added Elemtns to arraylist5
Added Elemtns to arraylist6
Added Elemtns to arraylist7
Added Elemtns to arraylist8
Added Elemtns to arraylist9
Notifying All
Exception in thread "Producer" java.lang.IllegalMonitorStateException
    at java.lang.Object.notifyAll(Native Method)
    at Producer.run(Producer.java:32)
Display from producer0
Display from producer1
Display from producer2
Display from producer3
Display from producer4
Display from producer5
Display from producer6
Display from producer7
Display from producer8
Display from producer9

我做错了什么?

P.S:我已在wait()区域内进行synchronized调用,这是IllegalMonitorStateException的主要原因。

0 个答案:

没有答案
相关问题