MultiQueue.java 742 Bytes
Newer Older
Michele Fiori's avatar
Michele Fiori 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
package chatRoom;

import java.util.ArrayList;

public class MultiQueue {

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

    public synchronized void put(String message, int clientId) {
        buffer.add(new MultiMessage(message, clientId));
        notify();
    }

    public synchronized MultiMessage take() {
        MultiMessage 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;
    }
}