5.8 go 解決多任務資源競爭問題

/**
 兩人同時打印,競爭資源
 輸出:
F:\goWorkSpace\study\05協程>go run 08_解決多任務資源競爭問題.go
wheolrlldo  
  並不是helloword,爲了解決此問題;後續我們來學習chanel

*/

package main
import (
	"fmt"
	"time"
)
func Printer(str string){
	for _,data:=range str{
		fmt.Printf("%c",data)
		time.Sleep(time.Second)
	}
	
	
}
func person1(){
	Printer("hello")
}
func person2(){
	Printer("world")
}

func main(){
	//新建兩個協程,代表兩個人去打印,2個人同時使用打印機
	go person1()
	go person2()
	
	for{}
	
	
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章