GO語言面試系列:(六)子goroutine與主線程的同步及面試編碼一例

如何實(shí)現(xiàn)子goroutine與主線程的同步

1、第一種方式:time.sleep():

package main

import (
        "fmt"
        "time"
)

func printCount() {
        for i := 0; i < 10; i++ {
            fmt.Println(i)
        }
}

func main() {
        go printCount()
        time.Sleep(time.Millisecond * 100)
}

這種方式?jīng)]法正確預(yù)估goroutine執(zhí)行的時(shí)間,很死板,不推薦使用。

2、第二種方式:使用channel機(jī)制,每個(gè)goroutine傳一個(gè)channel進(jìn)去然后往里寫數(shù)據(jù),在再主線程中讀取這些channel,直到全部讀到數(shù)據(jù)了子goroutine也就全部運(yùn)行完了,那么主goroutine也就可以結(jié)束了。這種模式是子線程去通知主線程結(jié)束。

package main

import (
        "fmt"
)

func printCount(rs chan int) {
        defer close(rs)
        for i := 0; i < 10; i++ {
                rs <- i
        }
}

func main() {
        rs := make(chan int, 10)
        go printCount(rs)

        for v := range rs {
                fmt.Println(v)
        }
}

3、第三種方式:使用context中cancel函數(shù),這種模式是主線程去通知子線程結(jié)束。

package main

import (
        "context"
        "fmt"
)

func printCount(ctx context.Context) chan int {
        dst := make(chan int)
        n := 0
        go func() {
                for {
                        select {
                                case <-ctx.Done():
                                        return
                                case dst <- n:
                                        n++
                        }
                }
        }()
        return dst
}

func main() {
        ctx, cancel := context.WithCancel(context.Background())
        defer cancel()

        intChan := printCount(ctx)
        for n := range intChan {
                if n == 9 {
                        break
                }
                fmt.Println(n)
        }
}

4、第四種方式:sync.WaitGroup模式,Add方法設(shè)置等待子goroutine的數(shù)量,使用Done方法設(shè)置等待子goroutine的數(shù)量減1,當(dāng)?shù)却臄?shù)量等于0時(shí),Wait函數(shù)返回。

package main

import (
        "fmt"
        "sync"
)

func printCount(wgp *sync.WaitGroup) {
        defer wgp.Done()
        for i := 0; i < 10; i++ {
                fmt.Println(i)
        }
}

func main() {
        wgp := &sync.WaitGroup{}
        wgp.Add(1)
        go printCount(wgp)
        wgp.Wait()
}

案例一:

  1. 問題描述 使用兩個(gè) goroutine 交替打印序列,一個(gè) goroutinue 打印數(shù)字, 另外一個(gè)goroutine打印字母, 最終效果如下 12AB34CD56EF78GH910IJ 。
  2. 解題思路 問題很簡單,使用 channel來控制打印的進(jìn)度。使用兩個(gè)channel ,來分別控制數(shù)字和字母的打印序列, 數(shù)字打印完成后通過 channel 通知字母打印, 字母打印完成后通知數(shù)字打印,然后周而復(fù)始的工作。
  3. 代碼實(shí)現(xiàn)
package main

import (
        "fmt"
        "runtime"
)

func main() {
        runtime.GOMAXPROCS(runtime.NumCPU())
        chan_n := make(chan bool)
        chan_c := make(chan bool, 1)
        done := make(chan struct{})

        go func() {
                for i := 1; i < 11; i += 2 {
                        <-chan_c
                        fmt.Printf("%d", i)
                        fmt.Printf("%d", i+1)
                        chan_n <- true
                }
        }()

        go func() {
                char_seq := [...]string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}
                for i := 0; i < 10; i += 2 {
                        <-chan_n
                        fmt.Printf("%s", char_seq[i])
                        fmt.Printf("%s", char_seq[i+1])
                        chan_c <- true
                }
                done <- struct{}{}
        }()

        chan_c <- true
        <-done
}

代碼執(zhí)行結(jié)果:

12AB34CD56EF78GH910IJ

聲明:文章來源多來自互聯(lián)網(wǎng),如果您感覺文章侵犯您的個(gè)人著作權(quán),請(qǐng)聯(lián)系我們,會(huì)在第一時(shí)間刪除。

來源:https://studygolang.com/articles/10786

關(guān)注小編微信:grey0805,了解更多哦!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容