Golang 代码并发问题

Golang ticker concurrency issue

我有一个用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 总是执行到循环中的最后一项?我该如何克服这个问题

对我有用(我发布这段代码时没有看到你是如何调用 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