goroutine Profile

About a .goroutine

  • Golang most attractive advantage is that from a linguistic level to support concurrent
  • goroutine (coroutines) in Golang is similar thread in other languages
  • Concurrent and Parallel
    • Parallel (Parallelism) means a different code segments supports on different physical processors
    • Concurrency (concurrency) refers to simultaneously manage multiple things that could run a half after the content on other things on the physical processor
    • In general it seems better than the performance of concurrent parallel because the physical resources of the computer is fixed, with less, and the content of the program need to do is a lot. So concurrency is "with fewer resources to do more things."
  • Several mainstream concurrency model
    • Multiple threads, each process only one request, and only after the request, the threads will be receiving the next request. In this mode the high concurrency, great performance overhead.
    • It may produce a large number of callback callback-based asynchronous IO in the course of running and maintenance costs lead to increased program execution flow is not easy thinking
    • . Coroutine does not require pre-emptive call, can effectively improve thread concurrency task, to make up for the shortcomings of multi-threaded mode; Golang to support the language level, but little support in other languages ​​(kernel mode and user mode)
  • goroutine syntax
    • The expression can be a single statement
    • The expression can also be a function, the function returns a value, if any, is invalid, when the function execution is completed automatically end this goroutine
      go 表达式

    II. Code Example

  • Contrast multiple calls to the function and effect of the use of goroutine
package main

import "fmt"
import "time"

func main() {
   //正常调用,输出3遍1 2 3 4 5(每个数字后换行)
   //for i:=1; i<=3; i++ {
   // go demo()
   //}

   /*
   添加go关键字后发现控制台什么也没有输出
   原因:把demo()设置到协程后没等到函数执行,主
   线程执行结束
    */
   for i := 1; i <= 3; i++ {
      go demo(i)
   }
}

func demo(index int) {
   for i := 1; i <= 5; i++ {
      fmt.Printf("第%d次执行,i的值为:%d\n", index, i)
   }
}
  • Add goroutine dormant waiting for the end of execution
  • In this way big problem is sleep time, sleep time if set too small, it may goroutine not executed, if the sleep time is set too large, the impact of the program execution. The execution of the sleep time to find the next program execution when the sleep time may be "too big" or "too small"
  • It found that each execution results are not necessarily the same, because each result by running the demo program () are concurrent execution
package main

import "fmt"
import "time"

func main() {
   //正常调用,输出3遍1 2 3 4 5(每个数字后换行)
   //for i:=1; i<=3; i++ {
   // go demo()
   //}

   /*
   添加go关键字后发现控制台什么也没有输出
   原因:把demo()设置到协程后没等到函数执行,主
   线程执行结束
    */
   for i := 1; i <= 3; i++ {
      go demo(i)
   }

   /*
   添加休眠,让主线程等待协程执行结束.
   具体休眠时间需要根据计算机性能去估计
   次数没有固定值
    */
   time.Sleep(3e9)
   fmt.Println("程序执行结束")
}

func demo(index int) {
   for i := 1; i <= 5; i++ {
      fmt.Printf("第%d次执行,i的值为:%d\n", index, i)
   }
}

Guess you like

Origin www.cnblogs.com/ronnieyuan/p/11780121.html