介紹安全
線程是cpu調度的最小單位,只有不一樣的線程才能同時在多核cpu上同時運行。但線程太佔資源,線程調度開銷大。go中的goroutine是一個輕量級的線程,執行時只須要4-5k的內存,比線程更易用,更高效,更輕便,調度開銷比線程小,可同時運行上千萬個併發。 go語言中開啓一個goroutine很是簡單,go函數名(),就開啓了個線程。併發
默認狀況下,調度器僅使用單線程,要想發揮多核處理器的並行處理能力,必須調用runtine.GOMAXPROCS(n)來設置可併發的線程數,也能夠經過設置環境變量GOMAXPROCS打到相同的目的。函數
goroutineui
Runtime包中提供了幾個與goroutine相關的函數。Gosched()讓當前正在執行的goroutine放棄CPU執行權限。調度器安排其餘正在等待的線程運行。 請看如下例子:this
package main線程
import ( "runtime" "fmt" )3d
func main(){ go sayHello() go sayWorld() var str string fmt.Scan(&str) }調試
func sayHello(){ for i := 0; i < 10; i++{ fmt.Print("hello ") runtime.Gosched() } }隊列
func sayWorld(){ for i := 0; i < 10; i++ { fmt.Println("world") runtime.Gosched() } } 運行結果 從上面輸出結果可知,咱們啓動了兩個線程,其中一個線程輸出一句後調用Gosched函數,釋放CPU權限;以後另外一個線程得到CPU權限。這樣兩個線程交替得到cpu權限,才輸出了以上結果。進程
runtime.NumCPU()返回了cpu核數,runtime.NumGoroutine()返回當前進程的goroutine線程數。即使咱們沒有開啓新的goroutine。
package main
import ( "runtime" "fmt" ) func main(){ fmt.Println(runtime.NumCPU()) fmt.Println(runtime.NumGoroutine()) } 運行結果 runtime.Goexit()函數用於終止當前的goroutine,單defer函數將會繼續被調用。
package main
import ( "runtime" "fmt" )
func test(){ defer func(){ fmt.Println(" in defer") }() for i := 0; i < 10; i++{ fmt.Print(i) if i > 5{ runtime.Goexit() } } }
func main(){ go test() var str string fmt.Scan(&str) } 運行結果 在這裏你們或許有個疑問,下面這兩句代碼幹嗎的呢
var str string fmt.Scan(&str) 這兩句代碼是等待輸入的意思,在這裏用來阻止主線程關閉的。若是沒有這兩句的話,會發現咱們的程序瞬間就結束了,並且什麼都沒有輸出。這是由於主線程關閉以後,全部開啓的goroutine都會強制關閉,他尚未來得及輸出,就結束了。 可是這樣感受怪怪的。若是有一種機制,在子線程結束的時候通知一下主線程,而後主線程再關閉,豈不是更好,這樣就不用無休止的等待了。因而就有了channel。
channel
goroutine之間經過channel來通信,能夠認爲channel是一個管道或者先進先出的隊列。你能夠從一個goroutine中向channel發送數據,在另外一個goroutine中取出這個值。 使用make建立
var channel chan int = make(chan int) // 或 channel := make(chan int) 生產者/消費者是最經典的使用示例。生產者goroutine負責將數據放入channel,消費者goroutine從channel中取出數據進行處理。
package main
import ( "fmt" )
func main(){ buf:=make(chan int) flg := make(chan int) go producer(buf) go consumer(buf, flg) <-flg //等待接受完成 }
func producer(c chan int){ defer close(c) // 關閉channel for i := 0; i < 10; i++{ c <- i // 阻塞,直到數據被消費者取走後,才能發送下一條數據 } }
func consumer(c, f chan int){ for{ if v, ok := <-c; ok{ fmt.Print(v) // 阻塞,直到生產者放入數據後繼續讀取數據 }else{ break } } f<-1 //發送數據,通知main函數已接受完成 } 運行結果
能夠將channel指定爲單向通訊。好比<-chan int僅能接收,chan<-int僅能發送。以前的生產者消費者能夠改成一下方式: func producer(c chan<-int){ defer close(c) // 關閉channel for i := 0; i < 10; i++{ c <- i // 阻塞,直到數據被消費者取走後,才能發送下一條數據 } }
func consumer(c <-chan int, f chan<-int){ for{ if v, ok := <-c; ok{ fmt.Print(v) // 阻塞,直到生產者放入數據後繼續讀取數據 }else{ break } } f<-1 //發送數據,通知main函數已接受完成 } channle能夠是帶緩衝的。make的第二個參數做爲緩衝長度來初始化一個帶緩衝的channel:
c := make(chan int, 5) 向帶緩衝的channel發送數據時,只有緩衝區滿時,發送操做纔會被阻塞。當緩衝區空時,接收纔會阻塞。 能夠經過如下程序調整發送和接收的順序調試
package main
import ( "fmt" )
func main(){ c := make(chan int, 2) c <- 1 c <- 2 fmt.Println(<-c) fmt.Println(<-c) } select
若是有多個channel須要監聽,能夠考慮用select,隨機處理一個可用的channel
package main
import ( "fmt" )
func main(){ c := make(chan int) quit := make(chan int) go func(){ for i := 0; i < 10; i++{ fmt.Printf("%d ", <-c) } quit <- 1 }() testMuti(c, quit) }
func testMuti(c, quit chan int){ x, y := 0, 1 for { select{ case c<-x: x, y = y, x+y case <-quit: fmt.Print("\nquit") return } } } 運行結果 channle超時機制
當一個channel被read/write阻塞時,會被一直阻塞下去,直到channel關閉。產生一個異常退出程序。channel內部沒有超時的定時器。但咱們能夠用select來實現channel的超時機制
package main
import ( "time" "fmt" )
func main(){ c := make(chan int) select{ case <- c: fmt.Println("沒有數據") case <-time.After(5* time.Second): fmt.Println("超時退出") } } 運行結果 線程同步
假設如今咱們有兩個線程,一個線程寫文件,一個線程讀文件。若是在讀文件的同時,寫文件的線程向文件中寫數據,就會出現問題。爲了保證可以正確的讀寫文件,在讀文件的時候,不能進行寫入文件的操做,在寫入時,不能進行讀的操做。這就須要互斥鎖。互斥鎖是線程間同步的一種機制,用了保證在同一時刻只用一個線程訪問共享資源。go中的互斥鎖在sync包中。下面是個線程安全的map:
package main
import ( "errors" "sync" "fmt" )
func main(){ m := &MyMap{mp:make(map[string]int), mutex:new(sync.Mutex)} go SetValue(m) go m.Display() var str string fmt.Scan(&str) }
type MyMap struct{ mp map[string]int mutex *sync.Mutex }
func (this *MyMap)Get(key string)(int, error){ this.mutex.Lock() i, ok := this.mp[key] this.mutex.Unlock() if !ok{ return i, errors.New("不存在") } return i, nil }
func (this *MyMap)Set(key string, val int){ this.mutex.Lock() defer this.mutex.Unlock() this.mp[key] = val }
func (this *MyMap)Display(){ this.mutex.Lock() defer this.mutex.Unlock() for key, val := range this.mp{ fmt.Println(key, "=", val) } }
func SetValue(m *MyMap){ var a rune a = 'a' for i := 0; i< 10; i++{ m.Set(string(a+rune(i)), i) } } 運行結果 完
做者:ChainZhang 連接:https://www.jianshu.com/p/c3d65105fa46 來源:簡書 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。