亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Golang 代碼并發問題

Golang 代碼并發問題

Go
一只斗牛犬 2022-05-18 13:41:48
我有一個用 Golang 編寫的函數,如下所示func (participant *SimulationParticipant) StartTransactionsGatewayTicker() {//Gatewaylogging.InfoLogger.Printf("StartTransactionsGatewayTicker:%v", participant.Participant)ticker := time.NewTicker(1 * time.Second)participant.TransactionGatewayTicker = tickergo func() {    for {        select {        case <-ticker.C:            logging.InfoLogger.Printf("Tick at: %v", participant.Participant)            participant.GetTransactions()        }    }}()}我在循環中調用該函數,就像數組中的 2 SimulationParticipant 一樣。令人驚訝的是,第一個參與者被第二個參與者取代,并且 GetTransactions 總是被執行到循環中的最后一項?我怎樣才能克服這個
查看完整描述

1 回答

?
狐的傳說

TA貢獻1804條經驗 獲得超3個贊

對我有用(我發布此代碼時沒有看到您如何調用 StartTransactionsGatewayTicker,如果不適用,請勿投反對票:P):


// [Timers](timers) are for when you want to do

// something once in the future - _tickers_ are for when

// you want to do something repeatedly at regular

// intervals. Here's an example of a ticker that ticks

// periodically until we stop it.


package main


import (

    "fmt"

    "time"

)


func main() {


        part1 := SimulationParticipant{}

    part1.id = "part1"

        part2 := SimulationParticipant{}

        part2.id = "part2"

        partSlice := make([]*SimulationParticipant,0)

        partSlice = append(partSlice, &part1, &part2)


        for _ , p := range partSlice {

             p.StartTransactionsGatewayTicker()

        }


    // Tickers can be stopped like timers. Once a ticker

    // is stopped it won't receive any more values on its

    // channel. We'll stop ours after 16000ms.

    time.Sleep(16000 * time.Millisecond)

    part1.ticker.Stop()

    part2.ticker.Stop()

    fmt.Println("Ticker stopped")

}


type SimulationParticipant struct {

     id string

     ticker *time.Ticker

}


func (participant *SimulationParticipant) StartTransactionsGatewayTicker() {


ticker := time.NewTicker(1 * time.Second)

participant.ticker = ticker

go func() {

    for {

        select {

        case t := <-ticker.C:

            fmt.Println("Tick at", t,participant.id)

        }

    }

}()

}


輸出 :


Tick at 2009-11-10 23:00:01 +0000 UTC m=+1.000000001 part2

Tick at 2009-11-10 23:00:01 +0000 UTC m=+1.000000001 part1

Tick at 2009-11-10 23:00:02 +0000 UTC m=+2.000000001 part1

Tick at 2009-11-10 23:00:02 +0000 UTC m=+2.000000001 part2

Tick at 2009-11-10 23:00:03 +0000 UTC m=+3.000000001 part2

Tick at 2009-11-10 23:00:03 +0000 UTC m=+3.000000001 part1

Tick at 2009-11-10 23:00:04 +0000 UTC m=+4.000000001 part1

Tick at 2009-11-10 23:00:04 +0000 UTC m=+4.000000001 part2

Tick at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001 part2

Tick at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001 part1

Tick at 2009-11-10 23:00:06 +0000 UTC m=+6.000000001 part1

Tick at 2009-11-10 23:00:06 +0000 UTC m=+6.000000001 part2

Tick at 2009-11-10 23:00:07 +0000 UTC m=+7.000000001 part2

Tick at 2009-11-10 23:00:07 +0000 UTC m=+7.000000001 part1

Tick at 2009-11-10 23:00:08 +0000 UTC m=+8.000000001 part1

Tick at 2009-11-10 23:00:08 +0000 UTC m=+8.000000001 part2

Tick at 2009-11-10 23:00:09 +0000 UTC m=+9.000000001 part2

Tick at 2009-11-10 23:00:09 +0000 UTC m=+9.000000001 part1

Tick at 2009-11-10 23:00:10 +0000 UTC m=+10.000000001 part1

Tick at 2009-11-10 23:00:10 +0000 UTC m=+10.000000001 part2

Tick at 2009-11-10 23:00:11 +0000 UTC m=+11.000000001 part2

Tick at 2009-11-10 23:00:11 +0000 UTC m=+11.000000001 part1

Tick at 2009-11-10 23:00:12 +0000 UTC m=+12.000000001 part1

Tick at 2009-11-10 23:00:12 +0000 UTC m=+12.000000001 part2

Tick at 2009-11-10 23:00:13 +0000 UTC m=+13.000000001 part2

Tick at 2009-11-10 23:00:13 +0000 UTC m=+13.000000001 part1

Tick at 2009-11-10 23:00:14 +0000 UTC m=+14.000000001 part1

Tick at 2009-11-10 23:00:14 +0000 UTC m=+14.000000001 part2

Tick at 2009-11-10 23:00:15 +0000 UTC m=+15.000000001 part2

Tick at 2009-11-10 23:00:15 +0000 UTC m=+15.000000001 part1

Ticker stopped

游樂場: https: //play.golang.org/p/yfHnrRK1iG8


查看完整回答
反對 回復 2022-05-18
  • 1 回答
  • 0 關注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號