This is a creation in Article, where the information may have evolved or changed.
Func say (s string) {
For I: = 0; I < 2; i++ {
Runtime. Gosched ()
Fmt. PRINTLN (s)
}
}
Func Main () {
Go Say ("World")
Say ("Hello")
}
Output:
Hello
World
Hello
1, the first output of Hello, after the output of the world
2, hello output 2, World output 1 (because the 2nd Hello output, the main thread exited, the 2nd world did not have a chance)
If you put the runtime in the code. Gosched () Comment out and execute the result as follows:
Hello
Hello
Because say ("hello") This sentence takes up time, when it executes, the thread ends, and say ("World") has no chance.
As can be seen from here, Goroutine is not really "parallel", goroutine are in a thread, between them through the continuous letting out the time slices in turn to run, to achieve similar results.