JAVA并發(fā)編程有界緩存的實(shí)現(xiàn)詳解
更新時(shí)間:2016年12月03日 16:04:47 投稿:lqh
這篇文章主要介紹了JAVA并發(fā)編程有界緩存的實(shí)現(xiàn)詳解的相關(guān)資料,這里舉例說明如何實(shí)現(xiàn),四種方法一一代碼實(shí)現(xiàn),需要的朋友可以參考下
JAVA并發(fā)編程有界緩存的實(shí)現(xiàn)
1、有界緩存的基類
package cn.xf.cp.ch14; /** * *功能:有界緩存實(shí)現(xiàn)基類 *時(shí)間:下午2:20:00 *文件:BaseBoundedBuffer.java *@author Administrator * * @param <V> */ public class BaseBoundedBuffer<V> { private final V[] buf; private int tail; private int head; private int count; public BaseBoundedBuffer(int capacity) { //初始化數(shù)組 this.buf = (V[]) new Object[capacity]; } //放入一個(gè)數(shù)據(jù),final方法無法被重寫 protected synchronized final void doPut(V v) { buf[tail] = v; if(++tail == buf.length) { tail = 0; } //插入一個(gè)方法,總量++ ++count; } /** * 取出一個(gè)數(shù)據(jù) * @return */ protected synchronized final V doTake() { V v = buf[head]; buf[head] = null; if(++head == buf.length) { head = 0; } --count; return v; } //通過對(duì)count的判斷,來確定數(shù)組是否是滿的 public synchronized final boolean isFull() { return count == buf.length; } public synchronized final boolean isEmpty() { return count == 0; } }
2、判定前提條件再執(zhí)行操作
package cn.xf.cp.ch14; /** * *功能:對(duì)插入和獲取元素操作進(jìn)行先行檢查,然后執(zhí)行操作,校驗(yàn)不通過不予操作 *時(shí)間:下午2:33:41 *文件:GrumpyBoundedBuffer.java *@author Administrator * * @param <V> */ public class GrumpyBoundedBuffer<V> extends BaseBoundedBuffer<V> { public GrumpyBoundedBuffer(int size) { super(size); } public synchronized void put(V v) throws Exception { //如果是滿的隊(duì)列,就無法插入新的元素 if(this.isFull()) { throw new Exception("隊(duì)列超出"); } this.doPut(v); } //同理,隊(duì)列為空的就無法取出新的元素 public synchronized V take() throws Exception { if(this.isEmpty()) { throw new Exception("隊(duì)列中無元素"); } return this.doTake(); } }
3、通過輪詢與休眠來實(shí)現(xiàn)簡單的阻塞
package cn.xf.cp.ch14; /** * *功能:通過輪詢與休眠來實(shí)現(xiàn)簡單的阻塞 *時(shí)間:下午2:55:54 *文件:SleepyBoundedBuffer.java *@author Administrator * * @param <V> */ public class SleepyBoundedBuffer<V> extends BaseBoundedBuffer<V> { //2s private static final long SLEEP_GRANULARITY = 2000; public SleepyBoundedBuffer(int capacity) { super(capacity); } //放入隊(duì)列的時(shí)候 public void put(V v) throws InterruptedException { while(true) { //這里不對(duì)循環(huán)上鎖,不然這個(gè)鎖就無法釋放了,不對(duì)休眠上鎖,休眠上鎖,在休眠的時(shí)候別人也無法操作,永遠(yuǎn)都不可能有元素出去 synchronized (this) { //如果隊(duì)列不是滿的,那么就放入元素 if(!this.isFull()) { this.doPut(v); return; } } //否則休眠,退出cpu占用 Thread.sleep(SLEEP_GRANULARITY); } } public V take() throws InterruptedException { while(true) { //這里不對(duì)循環(huán)上鎖,不然這個(gè)鎖就無法釋放了,不對(duì)休眠上鎖,休眠上鎖,在休眠的時(shí)候別人也無法操作,永遠(yuǎn)都不可能有新的元素進(jìn)來 synchronized(this) { //如果數(shù)組部位空,那么就可以取出數(shù)據(jù) if(!this.isEmpty()) { return this.doTake(); } //如果隊(duì)列為空,休眠幾秒再試 } Thread.sleep(SLEEP_GRANULARITY); } } }
4、條件隊(duì)列
package cn.xf.cp.ch14; /** * *功能:使用條件隊(duì)列 *時(shí)間:下午3:32:04 *文件:BoundedBuffer.java *@author Administrator * * @param <V> */ public class BoundedBuffer<V> extends BaseBoundedBuffer<V> { public BoundedBuffer(int capacity) { super(capacity); } /** * 放入數(shù)據(jù)元素 * @param v * @throws InterruptedException */ public synchronized void put(V v) throws InterruptedException { while(this.isFull()) { //這里掛起程序,會(huì)釋放鎖 this.wait(); } //如果隊(duì)列不為滿的,那么程序被喚醒之后從新獲取鎖 this.doPut(v); //執(zhí)行結(jié)束,喚醒其他隊(duì)列 this.notifyAll(); } public synchronized V take() throws InterruptedException { while(this.isEmpty()) { this.wait(); } V v = this.doTake(); this.notifyAll(); return v; } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
您可能感興趣的文章:
- Java 并發(fā)編程之線程掛起、恢復(fù)與終止
- java 高并發(fā)中volatile的實(shí)現(xiàn)原理
- java web如何解決瞬間高并發(fā)
- JAVA多線程并發(fā)下的單例模式應(yīng)用
- 簡單談?wù)凴xJava和多線程并發(fā)
- java高并發(fā)寫入用戶信息到數(shù)據(jù)庫的幾種方法
- Java 并發(fā)編程之ThreadLocal詳解及實(shí)例
- Java并發(fā)之不可思議的死循環(huán)詳解
- 淺談Java線程并發(fā)知識(shí)點(diǎn)
- Java 高并發(fā)十: JDK8對(duì)并發(fā)的新支持詳解
- Java 高并發(fā)八:NIO和AIO詳解
- 使用java的HttpClient實(shí)現(xiàn)多線程并發(fā)
- java并發(fā)之ArrayBlockingQueue詳細(xì)介紹
相關(guān)文章
使用ServletInputStream在攔截器或過濾器中應(yīng)用后重寫
這篇文章主要介紹了使用ServletInputStream在攔截器或過濾器中應(yīng)用后重寫,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10java實(shí)現(xiàn)兩臺(tái)服務(wù)器間文件復(fù)制的方法
這篇文章主要介紹了java實(shí)現(xiàn)兩臺(tái)服務(wù)器間文件復(fù)制的方法,是對(duì)單臺(tái)服務(wù)器上文件復(fù)制功能的升級(jí)與改進(jìn),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01JAVA中重寫(Override)與重載(Overload)的相關(guān)示例
這篇文章主要給大家介紹了關(guān)于JAVA中重寫(Override)與重載(Overload)的相關(guān)示例,重寫(override)和重載(overload)是兩種不同的方法重用技術(shù),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10Java 設(shè)計(jì)模式以虹貓藍(lán)兔的故事講解建造者模式
建造者模式,是一種對(duì)象構(gòu)建模式 它可以將復(fù)雜對(duì)象的建造過程抽象出來,使這個(gè)抽象過程的不同實(shí)現(xiàn)方法可以構(gòu)造出不同表現(xiàn)的對(duì)象。本文將通過示例講解建造者模式,需要的可以參考一下2022-04-04