go 無鎖隊列

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

無鎖隊列適用情境:

     兩個線程之間的互動資料, 一個線程生產資料, 另外一個線程消費資料,效率高

缺點:需要使用固定分配的空間,不能動態增加/減少長度,存在空間浪費和無法擴充空間問題

 

package mainimport (       "fmt"       "reflect"       "strings"       "time")
type LoopQueue struct{       start  int       end    int       length int       name   string       data   []interface{}}func (this* LoopQueue)InitQueue(length int, name string)bool{       if nil == this || length <= 0{              return false       }       this.data = make([]interface{}, length)       this.length = length       this.name = name       this.start = 0       this.end = 0       return true}func (this* LoopQueue)Push(data interface{})bool{       if nil == this{              panic("LoopQueue is nil")       }       if this.isFull(){              return false       }       var end int = this.getEnd()       this.data[end] = data       this.end = (end+1)%this.length       return true}func (this* LoopQueue)Pop() (bool, interface{}) {       if nil == this{              panic("LoopQueue is nil")       }       if this.isEmpty(){              return  false, nil       }       var start = this.getStart()       var startValue interface{} = this.data[start]       this.start = (start+1) % this.length       return true, startValue}func (this* LoopQueue)isEmpty()bool{       if nil == this{              panic("LoopQueue is nil")       }       if this.getStart() == this.getEnd(){              return true       }       return false}func (this* LoopQueue)isFull()bool{       if nil == this{              panic("LoopQueue is nil")       }       if this.getEnd() +1 == this.getStart(){              return true       }       return false}func (this* LoopQueue)getStart()int{       return this.start % this.length}func (this* LoopQueue)getEnd()int{       return this.end % this.length}var Q LoopQueuefunc Create(){       var index int = 0       for{              ret := Q.Push(index)              if ret{                     fmt.Println("PushOk", "index=", index)                     index++              }else{                     fmt.Println("PushError", "index=", index)              }              time.Sleep(1e9)       }}func Consum(){       for{              ret, data := Q.Pop()              if ret{                     fmt.Println("PopSucc", "data=",data)              }else{                     fmt.Println("PopError")              }              time.Sleep(1e9)       }}//實現環形隊列func main(){       Q.InitQueue(10, "test")       go Create()       go Consum()       for{              time.Sleep(1e9)       }}
569 次點擊  
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.