Queue.java 647 Bytes
Newer Older
Luca Arrotta's avatar
Luca Arrotta committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
package producerConsumer;

import java.util.ArrayList;

public class Queue {

    public ArrayList<String> buffer = new ArrayList<String>();

    public synchronized void put(String message) {
        buffer.add(message);
        notify();
    }

    public synchronized String take() {
        String message = null;

        while(buffer.size() == 0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if(buffer.size()>0){
            message = buffer.get(0);
            buffer.remove(0);
        }


        return message;
    }

}