在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ GO/ Go有狀態(tài)的goroutines實例
Go panic錯誤處理實例
Go命令行參數實例
Go可變參數的函數實例
Go通道同步實例
Go非阻塞通道操作實例
Go指針實例
Go數字解析實例
Go語言指針
Go超時(timeouts)實例
Go速率限制實例
Go信號實例
Go Base64編碼實例
Go計時器實例
Go命令行標志實例
Go原子計數器實例
Go語言切片
Go隨機數實例
Go語言類型轉換
Go排序實例
Go時間格式化/解析實例
Go URL解析實例
Go字符串函數實例
Go語言常量
Go for循環(huán)語句實例
Go函數多個返回值實例
Go切片實例
Go行過濾器實例
Go語言接口
Go語言數組
Go語言變量
Go字符串格式化實例
Go斷續(xù)器實例
Go if/else語句實例
Go通道緩沖實例
Go錯誤實例
Go語言映射
Go執(zhí)行過程實例
Go函數實例
Go有狀態(tài)的goroutines實例
Go按自定義函數排序實例
Go語言作用域規(guī)則
Go時代(Epoch)實例
Go變量實例
Go互斥體實例
Go語言范圍(range)
Go程序實例
Go語言入門
Go通道路線實例
Go閉包(匿名函數)實例
Go Select實例
Go通道范圍實例
Go集合函數實例
Hello World程序實例
Go環(huán)境變量實例
Go語言運算符
Go讀取文件實例
Go延遲(defer)實例
Go SHA1哈希實例
Go語言條件和決策
Go語言錯誤處理
Go通道實例
Go指針實例
Go時間日期實例
Go語言字符串
Go語言循環(huán)
Go語言基礎語法
Go語言開發(fā)環(huán)境安裝配置
Go常量實例
Go語言結構體
Go寫文件實例
Go正則表達式實例
Go JSON實例
Go語言教程
Go關閉通道實例
Go接口實例
Go語言遞歸
Go switch語句實例
Go函數遞歸實例
Go退出程序實例
Go語言程序結構
Go范圍實例
Go語言函數
Go工作池實例
Go語言數據類型

Go有狀態(tài)的goroutines實例

在前面的示例中,我們使用顯式鎖定互斥體來同步對多個goroutine的共享狀態(tài)的訪問。 另一個選項是使用goroutine和通道的內置同步功能來實現相同的結果。這種基于通道的方法與Go的共享內存的想法一致,通過溝通,擁有每個數據的goroutine恰好只有1個。

在這個例子中,狀態(tài)將由單個goroutine擁有。這將保證數據不會因并發(fā)訪問而損壞。為了讀或寫狀態(tài),其他goroutine將發(fā)送消息到擁有的goroutine并接收相應的回復。這些readOpwriteOp結構封裝了這些請求,并擁有一個goroutine響應的方法。

和以前一樣,我們將計算執(zhí)行的操作數。

讀寫通道將被其他goroutine分別用來發(fā)出讀和寫請求。

這里是擁有狀態(tài)的goroutine,它是一個如前面示例中的映射,但現在對狀態(tài)goroutine是私有的。這個goroutine在讀取和寫入通道時重復選擇,在請求到達時響應請求。 通過首先執(zhí)行所請求的操作,然后在響應信道上發(fā)送值以指示成功(以及在讀取的情況下的期望值)來執(zhí)行響應。

這里啟動了100goroutine來通過讀取通道向狀態(tài)擁有的goroutine發(fā)出讀取。每次讀取都需要構造一個readOp,通過讀取通道發(fā)送readOp,并通過提供的resp通道接收結果。

也使用類似的方法開始10個寫操作。讓goroutine工作一秒鐘。最后,捕獲和報告操作計數。

運行程序顯示,基于goroutine的狀態(tài)管理示例程序,完成了大約80,000次操作。

對于這種特殊情況,基于goroutine的方法比基于互斥的方法更多一些。它在某些情況下可能是有用的,例如,當有其他通道涉及或管理多個此類互斥體將容易出錯。應該使用最自然的方法,有助于理解程序。

所有的示例代碼,都放在 F:\worksp\golang 目錄下。安裝Go編程環(huán)境請參考:http://www.yiibai.com/go/go_environment.html

stateful-goroutines.go的完整代碼如下所示 -

package main

import (
    "fmt"
    "math/rand"
    "sync/atomic"
    "time"
)

// In this example our state will be owned by a single
// goroutine. This will guarantee that the data is never
// corrupted with concurrent access. In order to read or
// write that state, other goroutines will send messages
// to the owning goroutine and receive corresponding
// replies. These `readOp` and `writeOp` `struct`s
// encapsulate those requests and a way for the owning
// goroutine to respond.
type readOp struct {
    key  int
    resp chan int
}
type writeOp struct {
    key  int
    val  int
    resp chan bool
}

func main() {

    // As before we'll count how many operations we perform.
    var readOps uint64 = 0
    var writeOps uint64 = 0

    // The `reads` and `writes` channels will be used by
    // other goroutines to issue read and write requests,
    // respectively.
    reads := make(chan *readOp)
    writes := make(chan *writeOp)

    // Here is the goroutine that owns the `state`, which
    // is a map as in the previous example but now private
    // to the stateful goroutine. This goroutine repeatedly
    // selects on the `reads` and `writes` channels,
    // responding to requests as they arrive. A response
    // is executed by first performing the requested
    // operation and then sending a value on the response
    // channel `resp` to indicate success (and the desired
    // value in the case of `reads`).
    go func() {
        var state = make(map[int]int)
        for {
            select {
            case read := <-reads:
                read.resp <- state[read.key]
            case write := <-writes:
                state[write.key] = write.val
                write.resp <- true
            }
        }
    }()

    // This starts 100 goroutines to issue reads to the
    // state-owning goroutine via the `reads` channel.
    // Each read requires constructing a `readOp`, sending
    // it over the `reads` channel, and the receiving the
    // result over the provided `resp` channel.
    for r := 0; r < 100; r++ {
        go func() {
            for {
                read := &readOp{
                    key:  rand.Intn(5),
                    resp: make(chan int)}
                reads <- read
                <-read.resp
                atomic.AddUint64(&readOps, 1)
                time.Sleep(time.Millisecond)
            }
        }()
    }

    // We start 10 writes as well, using a similar
    // approach.
    for w := 0; w < 10; w++ {
        go func() {
            for {
                write := &writeOp{
                    key:  rand.Intn(5),
                    val:  rand.Intn(100),
                    resp: make(chan bool)}
                writes <- write
                <-write.resp
                atomic.AddUint64(&writeOps, 1)
                time.Sleep(time.Millisecond)
            }
        }()
    }

    // Let the goroutines work for a second.
    time.Sleep(time.Second)

    // Finally, capture and report the op counts.
    readOpsFinal := atomic.LoadUint64(&readOps)
    fmt.Println("readOps:", readOpsFinal)
    writeOpsFinal := atomic.LoadUint64(&writeOps)
    fmt.Println("writeOps:", writeOpsFinal)
}

執(zhí)行上面代碼,將得到以下輸出結果 -

F:\worksp\golang>go run mutexes.go
readOps: 84546
writeOps: 8473
state: map[0:99 3:3 4:62 1:18 2:89]

上一篇:Go語言入門下一篇:Go切片實例