为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 93 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号