This is a creation in Article, where the information may have evolved or changed. Golang has two very large features, that is, the goruntime and channel, these two features directly freed developers from concurrent and thread synchronization, so that high concurrency and thread synchronization between the code is very simple to write, and occupy less resources, synchronous transmission efficiency.
In terms of resource occupancy, the goroutine will grow or shrink memory consumption from the initial stack memory consumption of 4096 bytes as needed. Synchronous transmission efficiency, I once in song Hong's "Code of the Future" a concise example (in the Book of code in the end with a semicolon, the current Golang has canceled the source code in the end of the semicolon, by the compiler to add, a line of code containing multiple statements will need a semicolon delimited). The following code makes the appropriate adjustments based on the original code.
Package Mainimport ("FMT" "Time") func Chanflow (left, right Chan int, Bufferlen int.) {if Bufferlen <= 0 {left <-1 + & Lt;-right} else {for I: = 0, i < Bufferlen; i++ {left <-1 + <-right}}}func main () {nruntime: = 100000lastChan: = Make (chan int) var left chan int = nilright: = Lastchanbegin: = time. Now () Fmt. PRINTLN ("Begin at:", begin) for I: = 0; i < nruntime; i++ {left, right = right, make (chan int) go Chanflow (left, right, 0)}right <-0result: = <-lastchanend: = time. Now () Fmt. Println ("End at:", end, time.) Since (begin)) FMT. PRINTLN (Result)}
The program created 10w 1 unbuffered channel, 10w goruntime, data in Goruntime from the first channel to the last channel, each inflow once the value plus one. The code runs in my Notebook (2.7 GHz Intel Core i5, 8 GB 1867 MHz DDR3) with the following results:
Begin at:2016-08-28 14:42:04.972728029 +0800 CST
End at:2016-08-28 14:42:05.454288408 +0800 CST 481.560725ms
Takes less than half a second.
In the example above, the buffer channel is used, and the code is modified to have a channel with 1000 units buffered and try again, the code is as follows:
Func Chanflow (left, right Chan int, Bufferlen int) {...} Func Main () {nruntime: = 100000chanBuffer: = 1000result: = Make ([]int, 0, +) Lastchan: = Make (chan int, chanbuffer) var le ft Chan int = nilright: = Lastchanbegin: = time. Now () Fmt. PRINTLN ("Begin at:", begin) for I: = 0; i < nruntime; i++ {left, right = right, make (chan int, chanbuffer) go Chanflow (left, right, Chanbuffer)}for I: = 0; i < Chanbuffer; i++ {Right <-0}for I: = 0; i < Chanbuffer; i++ {result = Append (result, <-lastchan)}end: = time. Now () Fmt. Println ("End at:", end, time.) Since (begin)) FMT. PRINTLN (Result)}
The results of the operation are as follows:
Begin at:2016-08-28 14:54:09.352472708 +0800 CST
End at:2016-08-28 14:54:14.155240335 +0800 CST 4.802767822s
In less than 5 seconds, 1000 data passes through 10w of 1 channel in 10w Goruntime.
And in the actual production, more need to pass the data is a string, then the code again to try to modify, the code is as follows:
Package Mainimport ("Crypto/rand" "Encoding/base64" "FMT" "io" "Time") func Chanflow (left, right Chan string, Bufferlen int {if Bufferlen <= 0 {left <-<-right} else {for i: = 0; i < Bufferlen; i++ {left <-<-right}}}func gens Tring () string {b: = make ([]byte, +) If _, err: = Io. Readfull (Rand. Reader, B); Err! = Nil {return ""} else {return base64. Urlencoding.encodetostring (b)}}func main () {nruntime: = 100000chanBuffer: = 1000result: = Make ([]string, 0, +) Lastchan : = Make (chan string, chanbuffer) Dataforchan: = Do ([]string, 0, Chanbuffer) for I: = 0; i < Chanbuffer; i++ {Dataforchan = append (Dataforchan, genstring ())}var left Chan string = nilright: = Lastchanbegin: = time. Now () Fmt. PRINTLN ("Begin at:", begin) for I: = 0; i < nruntime; i++ {left, right = right, make (Chan string, chanbuffer) go Chanflow (left, right, Chanbuffer)}for I: = 0; i < Chanbuffer; i++ {Right <-dataforchan[i]}for I: = 0; i < Chanbuffer; i++ {result = Append (result, <-lastchan)}end: = Time. Now () Fmt. Println ("End at:", end, time.) Since (begin)) FMT. PRINTLN (Result)}
The results of the operation are as follows:
Begin at:2016-08-28 15:06:25.349599328 +0800 CST
End at:2016-08-28 15:06:31.288183546 +0800 CST 5.938584364s
In less than 6 seconds, 1000 x 44-byte random strings crossed 10w 1 channel in 10w Goruntime. The 1w 44-byte random string crosses the 1w 1 channel in 1w Goruntime for about 5 seconds.
As can be seen above, data in the Golang is very efficient in goruntime through channel synchronization.