This is the 13th day of my participation in the August More text Challenge. For details, see: August More Text Challenge
In my column:Let’s Golang
Before, we introduced some theoretical knowledge about Golang coroutine concurrency. Next, we will take you to understand the coroutine concurrency of Go through the code, and experience the million level concurrency of Go.
Let’s first understand the difference between process, thread and coroutine.
A process has its own heap and stack, while a thread has its own stack, but its heap is shared. The coroutines we’re going to talk about here are the same as threads, which share the heap but not the stack. But coroutines are not scheduled by the operating system, like processes and threads, but by programmers, which saves resources and improves performance.
Let’s see how the main coroutine and a coroutine perform in parallel.
func main(a) {
go func(a) {
for{
fmt.Println("i am coroutine")
time.Sleep(time.Second)
}
}()
β
for{
fmt.Println("I am the main coroutine")
time.Sleep(time.Second)
}
}
Copy the code
The result of their run is:
I'm a coroutine. I'm a coroutine. I'm a coroutineCopy the code
As we can see, they are not executed sequentially, but concurrently, which is coroutine concurrency.
func main(a) {
// But it is the main coroutine that starts 100 small coroutines
// Quickly open 100 small coroutines
for i:=0; i<10; i++{go doSomething("Bread Man"+strconv.Itoa(i))
}
β
for{
fmt.Println("I am the main coroutine")
time.Sleep(time.Second)
}
}
β
func doSomething(grname string) {
for{
fmt.Println("Here comes a bus.",grname)
time.Sleep(time.Second)
}
}
Copy the code
Here comes a busload of bread men 0, here comes a busload of bread men 7, here comes a busload of bread men 8, I'm the main coroutine, here comes a busload of bread men 2, here comes a busload of bread men 6, here comes a busload of bread men 3, here comes a busload of bread men 1, here comes a busload of bread men 5, here comes a busload of bread men 9, here comes a busload of bread men 4, here comes a busload of bread men 0 There's a busload of bread man 8, there's a busload of bread man 1, there's a busload of bread man 2, there's a busload of bread man 4, there's a busload of bread man 7, there's a busload of bread man 3, there's a busload of bread man 9, there's a busload of bread man 5, I'm the main coroutine, there's a busload of bread man 6, there's a busload of bread man 1, there's a busload of bread man 4Copy the code
This is 11 coroutines concurrent. It’s the main coroutine that invokes ten coroutines before you get a busload of breadwinners hitting you. So the main coroutine is the one who calls the bread man to hit you. So the main coroutine opens 10 coroutines. But the eleven coroutines are concurrent; they are not executed sequentially. Although they execute concurrently, they can only execute one coroutine at a time on the micro level, but their execution order is not consistent with the creation order.
then
for i:=0; i<10; i++{go doSomething("Bread Man"+strconv.Itoa(i))
}
Copy the code
Instead of
for i:=0; i<1000000; i++{go doSomething("Bread Man"+strconv.Itoa(i))
}
Copy the code
That’s Megabyte concurrency!
A main coroutine can have a million wagons hitting you.
After testing, the program lasted 1 minute without crashing. It turns out that executing coroutines consumes very little memory and CPU resources, so we can create a million coroutines. The 16G memory computer, using JAVA,C to do concurrency, almost also thousands of concurrent, while using the GO language, through the pipeline can make the concurrency ability to be greatly improved, we have achieved millions of concurrent.
Goroutine is cooperatively scheduled, whereas Java and others use threads that are preemptively scheduled.