All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.firebus.MessageQueue Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
package io.firebus;

public class MessageQueue
{
	protected Message[] messages;
	protected int head;
	protected int tail;
	
	public MessageQueue(int size)
	{
		messages = new Message[size];
		head = 0;
		tail = 0;
	}
	
	public synchronized void push(Message m)
	{
		messages[head++] = m;
		if(head >= messages.length)
			head = 0;
		if(head == tail)
			tail++;
		if(tail >= messages.length)
			tail = 0;
	}
	
	public synchronized int getMessageCount()
	{
		return (((head + messages.length) - tail) % messages.length);
	}
	
	
	public synchronized Message pop()
	{
		if(head == tail) {
			return null;
		} else {
			Message msg = messages[tail];
			messages[tail] = null;
			tail++;
			if(tail >= messages.length)
				tail = 0;
			return msg;
		}
	}	
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy