Golang 代码并发问题

我有一个用 Golang 编写的函数,如下所示


func (participant *SimulationParticipant) StartTransactionsGatewayTicker() {

//Gateway

logging.InfoLogger.Printf("StartTransactionsGatewayTicker:%v", participant.Participant)

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

participant.TransactionGatewayTicker = ticker

go func() {

    for {

        select {

        case <-ticker.C:

            logging.InfoLogger.Printf("Tick at: %v", participant.Participant)

            participant.GetTransactions()

        }

    }

}()

}

我在循环中调用该函数,就像数组中的 2 SimulationParticipant 一样。令人惊讶的是,第一个参与者被第二个参与者取代,并且 GetTransactions 总是被执行到循环中的最后一项?我怎样才能克服这个


一只斗牛犬
浏览 94回答 1
1回答

狐的传说

对我有用(我发布此代码时没有看到您如何调用 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 mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; &nbsp; &nbsp; part1 := SimulationParticipant{}&nbsp; &nbsp; part1.id = "part1"&nbsp; &nbsp; &nbsp; &nbsp; part2 := SimulationParticipant{}&nbsp; &nbsp; &nbsp; &nbsp; part2.id = "part2"&nbsp; &nbsp; &nbsp; &nbsp; partSlice := make([]*SimulationParticipant,0)&nbsp; &nbsp; &nbsp; &nbsp; partSlice = append(partSlice, &part1, &part2)&nbsp; &nbsp; &nbsp; &nbsp; for _ , p := range partSlice {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;p.StartTransactionsGatewayTicker()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; // Tickers can be stopped like timers. Once a ticker&nbsp; &nbsp; // is stopped it won't receive any more values on its&nbsp; &nbsp; // channel. We'll stop ours after 16000ms.&nbsp; &nbsp; time.Sleep(16000 * time.Millisecond)&nbsp; &nbsp; part1.ticker.Stop()&nbsp; &nbsp; part2.ticker.Stop()&nbsp; &nbsp; fmt.Println("Ticker stopped")}type SimulationParticipant struct {&nbsp; &nbsp; &nbsp;id string&nbsp; &nbsp; &nbsp;ticker *time.Ticker}func (participant *SimulationParticipant) StartTransactionsGatewayTicker() {ticker := time.NewTicker(1 * time.Second)participant.ticker = tickergo func() {&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case t := <-ticker.C:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Tick at", t,participant.id)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}()}输出 :Tick at 2009-11-10 23:00:01 +0000 UTC m=+1.000000001 part2Tick at 2009-11-10 23:00:01 +0000 UTC m=+1.000000001 part1Tick at 2009-11-10 23:00:02 +0000 UTC m=+2.000000001 part1Tick at 2009-11-10 23:00:02 +0000 UTC m=+2.000000001 part2Tick at 2009-11-10 23:00:03 +0000 UTC m=+3.000000001 part2Tick at 2009-11-10 23:00:03 +0000 UTC m=+3.000000001 part1Tick at 2009-11-10 23:00:04 +0000 UTC m=+4.000000001 part1Tick at 2009-11-10 23:00:04 +0000 UTC m=+4.000000001 part2Tick at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001 part2Tick at 2009-11-10 23:00:05 +0000 UTC m=+5.000000001 part1Tick at 2009-11-10 23:00:06 +0000 UTC m=+6.000000001 part1Tick at 2009-11-10 23:00:06 +0000 UTC m=+6.000000001 part2Tick at 2009-11-10 23:00:07 +0000 UTC m=+7.000000001 part2Tick at 2009-11-10 23:00:07 +0000 UTC m=+7.000000001 part1Tick at 2009-11-10 23:00:08 +0000 UTC m=+8.000000001 part1Tick at 2009-11-10 23:00:08 +0000 UTC m=+8.000000001 part2Tick at 2009-11-10 23:00:09 +0000 UTC m=+9.000000001 part2Tick at 2009-11-10 23:00:09 +0000 UTC m=+9.000000001 part1Tick at 2009-11-10 23:00:10 +0000 UTC m=+10.000000001 part1Tick at 2009-11-10 23:00:10 +0000 UTC m=+10.000000001 part2Tick at 2009-11-10 23:00:11 +0000 UTC m=+11.000000001 part2Tick at 2009-11-10 23:00:11 +0000 UTC m=+11.000000001 part1Tick at 2009-11-10 23:00:12 +0000 UTC m=+12.000000001 part1Tick at 2009-11-10 23:00:12 +0000 UTC m=+12.000000001 part2Tick at 2009-11-10 23:00:13 +0000 UTC m=+13.000000001 part2Tick at 2009-11-10 23:00:13 +0000 UTC m=+13.000000001 part1Tick at 2009-11-10 23:00:14 +0000 UTC m=+14.000000001 part1Tick at 2009-11-10 23:00:14 +0000 UTC m=+14.000000001 part2Tick at 2009-11-10 23:00:15 +0000 UTC m=+15.000000001 part2Tick at 2009-11-10 23:00:15 +0000 UTC m=+15.000000001 part1Ticker stopped游乐场: https: //play.golang.org/p/yfHnrRK1iG8
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go