Java Ring Buffer class __java

Source: Internet
Author: User
Import Java.util.LinkedHashMap; /** * @Description: Circularbuffer util * @author Administrator * @date 2017-5-1/public class CIRCULARBUFFER<T&G T {private object[] buffer = NULL;//data container private int capacity = 0;//buffer length private int indexforput = 0;//Put index (next The location to write to) private int indexforget = 0; Get index (next place to read)/** * Several states of the * thread * * Enum ThreadState {block,/* temporarily blocked/RUNNING,/* Run/over, * thread successfully completed
	* * INTERRUPT/* interrupt, (temporarily not used) * * * and put and get thread status tag/private threadstate putthreadstate = threadstate.running;
	Private ThreadState getthreadstate = threadstate.running;

	Linkedhashmap<integer, string> msgs = new Linkedhashmap<integer, string> ();
		/** * Specifies the length of the buffer * * @param capacity/public circularbuffer (int capacity) {this.capacity = capacity;
	This.buffer = new String[this.capacity]; /** * Write data, note that this function will cause blocking * * @param element/public void Putelement (T Element) {//////////////////////
	Vacancies can be inserted so far ~	while (null!= this.buffer[this.indexforput]) {try {this.setputthreadstate (threadstate.block);//The put thread is marked as: Blocking

			Thread.Sleep (100);

			catch (Interruptedexception e) {e.printstacktrace ();///Routine System.out.println ("thread stops unexpectedly, checking in"); } this.setputthreadstate (threadstate.running);
		The put thread is marked as: running/filling the element, pointing the putting index to the next element bit ~ This.buffer[this.indexforput] = element;
		this.indexforput++;
	This.indexforput%= this.capacity; /** * Fetch data, note that this function will block, if the put thread ends and the buffer is empty, the function returns NULL * * @return the next t element or null/@SuppressWarnings ("unchecked") pu Blic T getelement () {//have elements to take out ~//no elements on polling until the element can be taken up ~ If put finished, data empty, then return NULL to inform the caller while (null = = This.buffer[this.in  Dexforget]) {try {this.setgetthreadstate (threadstate.block);//Get thread marked as: Blocking if (This.isputedover () &&
					This.isemputy ()) {this.setgetthreadstate (threadstate.over);
				return null;

			} thread.sleep (100); catch (Interruptedexception e) {e.printstacktrace ();
				SYSTEM.OUT.PRINTLN ("Thread stops unexpectedly"); } this.setgetthreadstate (threadstate.running);
		Get thread is marked as: running//getting element, buffer freeing the element bit, pointing get index to the next element ~ */Object temp = This.buffer[this.indexforget];
		This.buffer[this.indexforget] = null;
		this.indexforget++;

		This.indexforget%= this.capacity; return (T) temp;  Returns the element reference that was obtained}/** * Check whether this ring buffer is empty * * @return Boolean true is null, FALSE is NOT NULL/public boolean isemputy () {//  The new element is put into the order of index 0 to the index length. For this reason, here is the upside down enumeration to prevent the occurrence of "same-chasing" as the result of the failure; for (int i = this.buffer.length-1 i > 0; i--)
			{if (this.buffer[i]!= null) {return false;
	} return true; /** * Check whether the put thread and get thread are blocked at the same time, * * @return boolean true-ball false-good/public boolean isallblock () {retur
	N (this.getthreadstate = = Threadstate.block && this.putthreadstate = = Threadstate.block); /** * Check that the data source is buffered * * @return Boolean true-complete false-not completed */public boolean isputedover () {return This.putt Hreadstate = = ThreAdstate.over;
	public void Setputthreadstate (ThreadState putthreadstate) {this.putthreadstate = putthreadstate;
	} private void Setgetthreadstate (ThreadState getthreadstate) {this.getthreadstate = getthreadstate;
 }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.