golang foundation - goconcurrency

goroutine is only a super "thread pool" implemented by the government. The stack memory consumption of 4-5kb p...
  • goroutine is only a super "thread pool" implemented by the government. The stack memory consumption of 4-5kb per instance and the creation and destruction cost for the implementation mechanism are greatly reduced.

  • Concurrency Is Not Parallelism (multi CPU)

  • Concurrency is mainly realized by switching time slices to run "at the same time", while concurrency is realized by using multi-core to run multi threads directly, but Go can set the number of cores to use to play the ability of multi-core computers.
    • Multithreading by go keyword
    package main import ( "fmt" "time" ) func Go() { fmt.Println("1234...") } func main() { go Go() //go keyword constitutes multithreading time.Sleep(2 * time.Second) //Main program sleep 2s }
  • Goroutine pursues sharing memory through communication, not sharing memory.

  • Channel
    • Channel is the bridge of goroutine communication, mostly blocking synchronous
    • Create through make, close (close automatically when the program is simple)
    package main import ( "fmt" ) func main() { //main program c := make(chan bool) //Initializing a chan type go func() { //subroutine fmt.Println("123...") //Execute main program c <- true //Save bool type to chan through < - }() fmt.Println(1) //Procedure execution step: 1st read_chan := <-c //< C Read bool from chan, program execution step: 2nd fmt.Println(read_chan) //Program execution steps: 3rd } /*output 1st 1 2nd 123... 3rd true */

    Note the execution order of the above programs (when there is no cache in the channel): first perform the read operation C < - C, because there is no value in the channel, so the program is blocked. At this time, perform the channel write operation, and then perform the read operation.

    • Channel is a reference type

    • You can use for range to iterate over and over channel s

    package main import ( "fmt" ) func main() { c := make(chan bool) //Initializing a chan type go func() { //go combines anonymous functions to construct concurrency fmt.Println("123...") //Execute main program c <- true //Save bool type to chan through < - close(c) //Close access: it must be clear where to close }() for v := range c { //for loop chanel } } /*output 123... true */
    • One way (read & write) or two-way channel can be set -- the default is two-way channel

    • You can set the cache size (0 by default, blocking), which will not block (asynchronously) until it is filled. For example, 20 caches can be cached, and 20 read or write operations can be performed at the same time. Note that read operations precede write operations

    package main import ( "fmt" ) func main() { //main program c := make(chan bool, 1) //Initialize a chan type with a cache of 2 go func() { //subroutine fmt.Println("123...") //Execute the main program, execute step: 2 c <- true //Write operation, execute step: 2 }() fmt.Println(2) //Execution step: 1 fmt.Println(123, <-c) //Read operation, execute step: 2 fmt.Println(3) //Execution step: 3 } /*output 1 2 2 123... 2 123 true 3 3 */

    After setting the cache, the program completes asynchronous, read and write operations at the same time. When there is no data in the read channal, it will not cause blocking, because at the same time, write operations will also occur.

9 February 2020, 10:31 | Views: 5313

Add new comment

For adding a comment, please log in
or create account

0 comments