Java環形緩衝區類__Java

來源:互聯網
上載者:User
import java.util.LinkedHashMap;/** * @Description: circularBuffer util * @author Administrator * @date 2017-5-1 */public class CircularBuffer<T> {private Object[] buffer = null; // 資料容器private int capacity = 0; // 緩衝區長度private int indexForPut = 0; // put索引 (下一個要寫入的位置)private int indexForGet = 0; // get索引 (下一個要讀取的位置)/** * 線程的幾種狀態 */enum ThreadState {BLOCK, /* 暫時阻塞 */RUNNING, /* 運行 */OVER, /* 線程順利完成 */INTERRUPT /* 中斷,(暫時用不到) */}/* put 和 get 的線程狀態標記 */private ThreadState putThreadState = ThreadState.RUNNING;private ThreadState getThreadState = ThreadState.RUNNING;LinkedHashMap<Integer, String> msgs = new LinkedHashMap<Integer, String>();/** * 指定長度的緩衝區 *  * @param capacity */public CircularBuffer(int capacity) {this.capacity = capacity;this.buffer = new String[this.capacity];}/** * 寫入資料,注意此函數會導致阻塞 *  * @param element */public void putElement(T element) {// 有空位就插入~// 沒空位就輪詢,直到有空位可插入為止~while (null != this.buffer[this.indexForPut]) {try {this.setPutThreadState(ThreadState.BLOCK); // put線程標記為:阻塞Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace(); // 例行公事System.out.println("線程意外停止,正在檢查");}}this.setPutThreadState(ThreadState.RUNNING); // put線程標記為:正在運行// 填入元素,將put索引指向下一元素位~this.buffer[this.indexForPut] = element;this.indexForPut++;this.indexForPut %= this.capacity;}/** * 取資料,注意此函數會阻塞,若put線程結束且緩衝區為空白時函數會返回null *  * @return 下一個T元素 或者 null */@SuppressWarnings("unchecked")public T getElement() {// 有元素就拿出~// 沒元素就輪詢,直到有元素可拿為止~ 若是put完畢、 資料取空,則返回null以告知調用者while (null == this.buffer[this.indexForGet]) {try {this.setGetThreadState(ThreadState.BLOCK); // get線程標記為:阻塞if (this.isPutedOver() && this.isEmputy()) {this.setGetThreadState(ThreadState.OVER);return null;}Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();System.out.println("線程意外停止");}}this.setGetThreadState(ThreadState.RUNNING); // get線程標記為:正在運行/* 拿到元素,buffer騰出該元素位,將get索引指向下一元素~ */Object temp = this.buffer[this.indexForGet];this.buffer[this.indexForGet] = null;this.indexForGet++;this.indexForGet %= this.capacity;return (T) temp; // 返回拿到的元素引用}/** * 檢查此環形緩衝區是否為空白 *  * @return boolean true則表示為空白,false則不為空白 */public boolean isEmputy() {// 新元素是以 索引0 向 索引length 的順序 put入// 有鑒於此,這裡倒過來枚舉,防止出現“同向追趕”導致落空的的囧事;for (int i = this.buffer.length - 1; i > 0; i--) {if (this.buffer[i] != null) {return false;}}return true;}/** * 檢查 put 線程和 get 線程是否同時阻塞, *  * @return boolean true-掛球 false-良好 */public boolean isAllBlock() {return (this.getThreadState == ThreadState.BLOCK && this.putThreadState == ThreadState.BLOCK);}/** * 檢查資料來源是否緩衝完畢 *  * @return boolean true-完成 false-未完成 */public boolean isPutedOver() {return this.putThreadState == ThreadState.OVER;}public void setPutThreadState(ThreadState putThreadState) {this.putThreadState = putThreadState;}private void setGetThreadState(ThreadState getThreadState) {this.getThreadState = getThreadState;}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.