1. 程式人生 > >自己實現阻塞佇列

自己實現阻塞佇列

照著ArrayBlockingQueue自己實現了一些阻塞佇列,其原理就是生產者消費者關係,先實現了功能,一些細節(為什麼不用wite,notify 為什麼不用lock.lock())暫時沒有去學習。

佇列程式碼:

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
 * Created on 2017/8/31.
 */
public class MyBlockingQueue {
    final Object[] items=new Object[3
]; final ReentrantLock lock=new ReentrantLock(); private final Condition notFull; private static int num=-1; public MyBlockingQueue() { this.notFull = lock.newCondition(); } public void put(Object item) throws InterruptedException { // 如果滿了則開始阻塞 lock.lockInterruptibly(); try
{ while(items.length==num+1){ System.out.println("queue is filled and begin to wite"); notFull.await(); } num++; items[num]=item; System.out.println("put .."+item); System.out.println("put success and try to notify"
); notFull.signal(); }finally { lock.unlock(); } } public Object take(int index) throws InterruptedException { if (items.length<index){ System.out.println("index is overflow...index="+index+",length="+items.length); return null; } // 為空則阻塞 lock.lockInterruptibly(); try { while(num==-1){ System.out.println("queue is empty and begin to wite"); notFull.await(); } Object result=items[num]; items[num]=null; System.out.println("queue`s "+num+"is already remove"); num--; System.out.println("try to notify"); notFull.signal(); return result; }finally { lock.unlock(); } } }

測試類程式碼:


/**
 * Created on 2017/8/31.
 */
public class Mytest {
    public static void main(String[] args) {
        final MyBlockingQueue queue=new MyBlockingQueue();
       new Thread(new Runnable() {
           @Override
           public void run() {
               for (int i = 0; i <10 ; i++) {
                   try {
                       queue.put(i);
                       Thread.sleep(10);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }
       }).start();
       new Thread(new Runnable() {
           @Override
           public void run() {
               for (int i = 0; i <10 ; i++) {
                   try {
                       queue.take(0);
                       Thread.sleep(100);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               }
           }
       }).start();
    }
}

列印結果:

put ..0
put success and try to notify
queue`s 0is already remove
try to notify
put ..1
put success and try to notify
put ..2
put success and try to notify
put ..3
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..4
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..5
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..6
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..7
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..8
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..9
put success and try to notify
queue`s 2is already remove
try to notify
queue`s 1is already remove
try to notify
queue`s 0is already remove
try to notify

相關推薦

自己實現阻塞佇列

照著ArrayBlockingQueue自己實現了一些阻塞佇列,其原理就是生產者消費者關係,先實現了功能,一些細節(為什麼不用wite,notify 為什麼不用lock.lock())暫時沒有去學習。

java 併發包 Lock Condition實現阻塞佇列

import java.util.LinkedList; import java.util.List; import java.util.Random; import java.util.concurrent.locks.Condition; import java.util.concurrent.

python實現阻塞佇列

怎麼實現阻塞佇列?當然是靠鎖,但是應該怎麼鎖?一把鎖能在not_empty,not_full,all_tasks_done三個條件之間共享。好比說,現在有執行緒A執行緒B,他們準備向佇列put任務,佇列的最大長度是5,執行緒A在put時,還沒完事,執行緒B就開始put,佇列就

Java實現阻塞佇列的兩種方式

方式一:/** * 使用非阻塞佇列PriorityQueue及wait/notify方法實現一個阻塞佇列**/class MyBlockingQueue {    public final static int queueSize = 10;   &

使用C++ 11 實現阻塞佇列

#pragma once #include <condition_variable> #include <mutex> #include <deque> template<class T> class BlockQueue { public: typedef

【小家java】BlockingQueue阻塞佇列詳解以及5大實現(ArrayBlockingQueue、DelayQueue、LinkedBlockingQueue...)

相關閱讀 【小家java】java5新特性(簡述十大新特性) 重要一躍 【小家java】java6新特性(簡述十大新特性) 雞肋升級 【小家java】java7新特性(簡述八大新特性) 不溫不火 【小家java】java8新特性(簡述十大新特性) 飽受讚譽 【小家java】java9

Java併發(十八):阻塞佇列BlockingQueue BlockingQueue(阻塞佇列)詳解 二叉堆(一)之 圖文解析 和 C語言的實現 多執行緒程式設計:阻塞、併發佇列的使用總結 Java併發程式設計:阻塞佇列 java阻塞佇列 BlockingQueue(阻塞佇列)詳解

阻塞佇列(BlockingQueue)是一個支援兩個附加操作的佇列。 這兩個附加的操作是:在佇列為空時,獲取元素的執行緒會等待佇列變為非空。當佇列滿時,儲存元素的執行緒會等待佇列可用。 阻塞佇列常用於生產者和消費者的場景,生產者是往佇列裡新增元素的執行緒,消費者是從佇列裡拿元素的執行緒。阻塞佇列就是生產者

阻塞佇列BlockingQueue以及它的兩個重要實現類ArrayBlockingQueue和LinkedBlockingQueue

多執行緒環境中,通過佇列可以很容易實現資料共享,比如經典的“生產者”和“消費者”模型中,通過佇列可以很便利地實現兩者之間的資料共享。假設我們有若干生產者執行緒,另外又有若干個消費者執行緒。如果生產者執行緒需要把準備好的資料共享給消費者執行緒,利用佇列的方式來傳遞資料,就可以很方便地解決他們之間的資料

Java併發(二十一):執行緒池實現原理 Java併發(十八):阻塞佇列BlockingQueue Java併發(十八):阻塞佇列BlockingQueue Java併發程式設計:執行緒池的使用

一、總覽 執行緒池類ThreadPoolExecutor的相關類需要先了解:  (圖片來自:https://javadoop.com/post/java-thread-pool#%E6%80%BB%E8%A7%88) Executor:位於最頂層,只有一個 execute(Runnab

自己實現的線性佇列

package hjj.queue; public class MyQueue<T> { private int size; private int capacity; private Object[] queue; public MyQueue() { this

jdk提供的阻塞佇列BlockingQueue下的五個實現簡單操作以及介紹

package cn.yarne.com.base.test; import java.util.Collections; import java.util.Date; import java.util.Iterator; import java.util.Vector;

什麼是阻塞佇列? 如何使用阻塞佇列實現生產者-消費者模型?

什麼是阻塞佇列? 阻塞佇列是一個在佇列基礎上又支援了兩個附加操作的佇列。 2個附加操作: 支援阻塞的插入方法:佇列滿時,佇列會阻塞插入元素的執行緒,直到佇列不滿。 支援阻塞的移除方法:佇列空時,獲取元素的執行緒會等待佇列變為非空。 阻塞佇列的應用場

基於阻塞佇列實現的簡單生產者-消費者模式

生產者:生成資料放入到佇列中,供消費者消費; 消費者:從佇列中獲取資料,進行消費。 下面是一個簡單的生產者-消費者模式程式碼例項: 生產者執行緒每隔3秒生產一個隨機數並放入到阻塞佇列中,消費者執行緒不斷得去佇列中獲取元素進行消費。 1、生產者程式碼 /** * @Description: 生

封裝一個阻塞佇列,輕鬆實現排隊執行任務功能!

前言 個人覺得佇列的使用在專案開發中挺多地方可以用到的,所以將如何封裝一個佇列的過程記錄下來,總體來說難度並不大,但畢竟能力有限,如果各位有好的建議或意見歡迎提出來,如果本文能幫到你的話,記得點贊哦。 需求背景 在專案開發中,會經常遇到一些需要排隊執行的功能,比如發動態時上傳多張圖片,需要一張一張的上傳

自己實現佇列【親測】

佇列類 public class MyQueue { private long []arr;//底層陣列實現 private int front;//隊頭 private int rear;//隊尾 private int size;//佇列中有效資

Java阻塞佇列實現生產者和消費者

生產者 import java.util.Random; import java.util.concurrent.BlockingQueue; //生產者 public class Producer implements Runnable{ private final Block

Java併發程式設計-阻塞佇列(BlockingQueue)的實現原理

阻塞佇列 (BlockingQueue)是Java util.concurrent包下重要的資料結構,BlockingQueue提供了執行緒安全的佇列訪問方式:當阻塞佇列進行插入資料時,如果佇列已滿,執行緒將會阻塞等待直到佇列非滿;從阻塞佇列取資料時,如果

Java多執行緒-生產者消費者例子-使用阻塞佇列(BlockingQueue)實現

import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; /** * Created by wisgood . */ public

自己動手實現訊息佇列之JMS

什麼是JMS?JMS的誕生史? 在JMS還沒有誕生前,每個企業都會有自己的一套內部訊息系統,比如專案組A需要呼叫到專案組B的系統,專案組B也有可能會呼叫到專案組C的系統。這樣每個公司都有自己的一套實現。很不規範,所以Apache基金會,為企業訊息產品專門定義了一

Java阻塞佇列實現

阻塞佇列與普通佇列的區別在於,當佇列是空的時,從佇列中獲取元素的操作將會被阻塞,或者當佇列是滿時,往佇列裡新增元素的操作會被阻塞。試圖從空的阻塞佇列中獲取元素的執行緒將會被阻塞,直到其他的執行緒往空的佇列插入新的元素。同樣,試圖往已滿的阻塞佇列中新增新元素的執行緒同樣也會