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

com.javanut.pronghorn.pipe.ObjectPipe Maven / Gradle / Ivy

Go to download

Ring buffer based queuing utility for applications that require high performance and/or a small footprint. Well suited for embedded and stream based processing.

There is a newer version: 1.1.27
Show newest version
package com.javanut.pronghorn.pipe;

import java.lang.reflect.Array;
import java.util.concurrent.atomic.AtomicInteger;

public class ObjectPipe {

	private final int size;
	private final int mask;	
	private final T[] objects;
	private final AtomicInteger head = new AtomicInteger(); //TODO: this can be optimized by caching the head
	private final AtomicInteger tail = new AtomicInteger(); //TODO: this can be optimized by caching the tail
	private int publicTail = 0;
	
	
	private Thread headThread; //for assert to ensure only 2 threads are used, one read and one write
	private Thread tailThread; //for assert to ensure only 2 threads are used, one read and one write
	
	public ObjectPipe(int bits, Class clazz, ObjectPipeObjectCreator opoc) {
		this.size = 1<=0) {
			objects[i] = opoc.newInstance();		
		}
	}
	

	/**
	 * 
	 * @return true if this can move
	 */
	public boolean tryMoveHeadForward() {
		assert(isHeadThread());
		if (count()0) {
			return objects[tail.get()&mask];
		} else {
			return null;
		}
	}

    private boolean isTailThread() {
		Thread t = Thread.currentThread();
		if (null == tailThread) {
			tailThread = t;
		}
		return t==tailThread;
	}


	////////////////////////////////
	
	public int count() {
		return head.get()-publicTail;
	}

	public boolean hasRoomFor(int count) {
		return (this.count()+count)<=mask;
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy