Finishing the golang debris lucky airship program concurrent production

Concurrency: the same period of time to perform multiple tasks.
Parallel: the same time to perform multiple tasks.

Fortunately airship program production q <115.28.8.00.9.9>

Go through the implementation of concurrent language goroutine. goroutine similar thread, the thread belonging to the user mode, we can create hundreds of thousands of concurrent goroutine work as needed. goroutine is done by running the scheduling of the language go, and the thread is done by the operating system scheduling.
Go language also provides a channel of communication carried out between multiple goroutine. goroutine and the channel is an important foundation to achieve language go to uphold the CSP (Communicating Sequential Process) concurrency model.

goroutine
time in Java / c ++, we want to achieve concurrent programming, we often want to protect their own thread pool, and to make their own packed one after another task and then themselves to schedule threads to perform tasks and maintain on-line text switch, all this usually spend a lot of mental programmers. Can not have a mechanism, the programmer only needs to define a number of tasks, allow the system to help us to assign these tasks to the CPU concurrency enforce it? Go language goroutine is such a mechanism, the reason can be called a modern language Go programming language, because it has built-in language level mechanism for scheduling and context switching.

Use goroutine
Go program to create a goroutine as a function of use go keyword. A function can be created multiple goroutine, a goroutine must correspond to a function.

Start a single goroutine
way to start goroutine is very simple, just add a keyword go in front of the called function (normal function and anonymous functions).
for example:

package main

import (
"fmt"
)

hello FUNC () {
fmt.Println ( "hello goroutines")
}
FUNC main () {
hello ()
fmt.Println ( "main goroutines DONE!")
}
in this example and the following statements hello function is serial, performing the result is that after printing the print hello goroutine main goroutine done!
Next we call the function hello plus go in front of the keyword, which is a start goroutine area to perform this function hello.

main FUNC () {
Go the Hello ()
fmt.Println ( "main goroutine DONE!")
}
This time the results of the implementation to print only the main goroutine done!, and did not print hello goroutine, why?
When the program starts, Go program will () function creates a default goroutine for the main. When the main function returns the goroutine is over, all () function to start goroutine will end with the main, goroutine main function where the king is like a night game in the right, the other like a different ghost goroutine those different ghost king to die night it will convert all of the GG.
So we find ways to make the main function wait hello function, the easiest way is to sleep rough up.

main FUNC () {
Go the Hello ()
fmt.Println ( "main goroutine DONE!")
the time.sleep (time.Second)
}
execute the above code you will find that this time the first print main goroutine done!, then immediately print Hello goroutine!
first, why print main goroutine done! because we need to spend some time in creating a new goroutine time, but this time goroutine where the main function is to continue to perform.

Guess you like

Origin www.cnblogs.com/jihuazhong/p/11250201.html