Golang concurrent programming of goroutine

Go concurrent programming language

Concurrency: the same to perform multiple tasks (you chat with a micro-channel and two girlfriends) period
in parallel: the same time to perform multiple tasks (you and your friends are chatting with a micro-channel and girlfriend)
concurrent language Go through the goroutinerealization . goroutine�Similar to the thread, the thread belonging to the user mode, we can create thousands of needed goroutinework concurrently. goroutineGo run by the language of the time (runtime) scheduling is completed, and the thread is done by the operating system scheduler

Go language is also provided channelin a plurality of goroutinecommunication between. goroutineAnd channelis an important foundation to achieve the Go language CSP concurrent mode of inheritance

goroutine Profile

In Java and C ++ concurrent programming we want to achieve, we must maintain a thread pool of its own, and to make their own packed one after another task, but also themselves to perform tasks and maintenance schedule threads context switching.

Go programming language you do not need to write their own processes, threads, coroutines, your skills package is only one skill goroutine, when you need to perform a task complicated by the time you just need this task packaged into a function, to open a goroutineto perform this function on it.

goroutine application

Go language used goroutineis very simple, just call functions in the front plus a gokeyword, a function that can create a goroutine.

A goroutinemust correspond to a function, you can create multiple goroutineto perform the same function

package main

import (
    "fmt"
    "time"
)

// goroutine
func hello(i int){
    fmt.Println("hello",i)
}

// 程序启动之后会创建一个主goroutine去执行
func main(){
    for i:=0;i<100;i++ {
        //go hello(i) // 开启一个单独的goroutine去执行hello函数(任务)
        go func(i int){
            fmt.Println(i)
        }(i)
    }
    fmt.Println("main")
    time.Sleep(time.Second)
    // main函数结束了 由main函数启动的goroutine也都结束了
}

Go concurrency in languages is so simple, we can also launch multiple goroutine. Let's take another example (used here sync.WaitGroup goroutine to achieve
synchronization)

package main

import (
    "fmt"
    "math/rand"
    "sync"
    "time"
)

func f(i int){
    defer wg.Done() // 计数器减1
    time.Sleep(time.Millisecond * time.Duration(rand.Intn(300)))
    fmt.Println(i)
}

var wg sync.WaitGroup

func main(){
    //f()
    for i:=0;i<10;i++{
        wg.Add(1)  // 计数器加1
        go f(i)
    }
    // 如何知道这10个goroutine都结束了
    wg.Wait() // 等待wg的计数器减为0

}

GOMAXPROCS

Go run-time scheduler will use the GOMAXPROCSparameters to determine how many need to use the OS threads to simultaneously execute the Go, the default value is the number of CPU cores on the machine.

package main

import (
    "fmt"
    "runtime"
    "sync"
)

var wg sync.WaitGroup

func a(){
    defer wg.Done()
    for i:=0;i<10;i++{
        fmt.Printf("A:%d\n",i)
    }
}

func b(){
    defer wg.Done()
    for i:=0;i<10;i++{
        fmt.Printf("B:%d\n",i)
    }
}

func main(){
    runtime.GOMAXPROCS(1) // 指定在N个核心数上调度
    wg.Add(2)
    go a()
    go b()
    wg.Wait()
}

Guess you like

Origin www.cnblogs.com/jasonminghao/p/12348277.html