1114. Print in Order 按序列印
The same instance ofFoowill be passed to three different threads. Thread A will callfirst(), thread B will callsecond(), and thread C will callthird(). Design a mechanism and modify the program to ensure thatsecond()is executed afterfirst(), andthird()is executed aftersecond().
本题要求阻塞一众并发的 Goroutine,然后按 One Two Three 的逻辑顺序列印结果
package goroutine import "fmt" var chFir = make(chan int) var chSec = make(chan int) var chThi = make(chan int) func printFir() { fmt.Print("first") chFir <- 1 } func printSec() { <-chFir fmt.Print("second") chSec <- 2 } func printThi() { <-chSec fmt.Print("third") chThi <- 3 } func PrintABC(arr []int) { // arr := []int{1,2,3} funcList := map[int]func(){1: printFir, 2: printSec, 3: printThi} for _, v := range arr { go funcList[v]() } <-chThi } 作者:li-er-dan-w 链接:https://leetcode-cn.com/problems/print-in-order/solution/golangxie-cheng-shi-xian-by-li-er-dan-w-kczv/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。