This is a creation in Article, where the information may have evolved or changed.
Reprint: http://c2pblog.sinaapp.com/archives/450
With the rapid development of hardware, the CPU has long been a multi-core, and how to handle concurrent programming to adapt to multicore CPUs is a point that every modern programming language pays attention to. Golang has been advocating concurrent programming since birth, and the native Goroutines and channel are concise support for complex parallel operations.
http://blog.csdn.net/gdutliuyun827/article/details/25145535
This article is a detailed analysis of the features of the go language, here to thank the original author.
The following side of the Code edge Analysis implementation process.
func showNum() {
for
i :=
0
; i <
30
; i++ {
fmt.Println(i)
atm := time.Duration(rand.Intn(
250
))
//延迟处理
time.Sleep(time.Millisecond * atm)
}
}
|
This is a simple function, print out 0 to 29, in order to be able to see more clearly, I did the related delay processing.
If the function is called directly in the main function, it can only be printed once, which is non-parallelized processing. So how does it become parallelization? It's simple, and it's good to add go in front of the call.
Go Shownum ()
Just like this. Since it's parallel, we're going to call 10 times this function to do an experiment.
for i: = 0 ; I < 10 &NBSP;&NBSP;&NBSP;&NBSP; } |
As a result, when you run, you find that nothing is running out.
Not run out, too fast, see, and every time go shownum results are discarded. So how do we see the results? Have to think of a way to get main to stop, then write this sentence:
vat in string fmt.Scanln(&in) |
Use standard input to enter a value for main. Add this sentence after you go to run will find that there are numbers in print, and Java in a similar thread, each time the printing is random, cannot be determined. This is the simplest goroutines.
Simply put, if you want to do parallel processing of any function, then you can call this function before adding the keyword go.
The second point to be said is the channel, which is used to achieve communication between the goroutine, that is, two goroutine exchange some information < of course, mainly related variables >. The way to define a channel is to do the following:
var c1 chan string = make(chan string) var c2 = make(chan string) c3 :=make(chan string) |
It's all possible. It is important to note that each channel must have a type and that this type cannot be used in a casual mix. Golang is a useful and vivid way to represent the transmission of information: this is <-.
input <- "lkn" //input 是一个chan string ,可以接受一个string data<-input //input是一个chan string ,用来传出一个string |
A general Chan can be passed in two directions, and if you define a single Chan that can only be passed in one direction, it can be written like this:var c2 = make (chan<-string) or var c2 = makes (<-chan String). The previous expression can only be passed in, and the latter represents only outgoing. Now let's use a simple example of printing prime numbers, combining goroutine and channel.
//素数生产者
func prime_generate() chan
int {
ch := make(chan
int
)
go func() {
for
i :=
2
; ; i++ {
ch <- i
}
}()
return
ch
}
//素数筛选器
func prime_filter(in chan
int
, prime
int
) chan
int {
out := make(chan
int
)
go func() {
for
{
if
i := <-in; i%prime !=
0
{
out <- i
}
}
}()
return
out
}
//素数消费者
func prime_sieve() chan
int {
out := make(chan
int
)
go func() {
ch := prime_generate()
for
{
prime := <-ch
out <- prime
ch = prime_filter(ch, prime)
}
}()
return
out
}
|
This is OK, the main function is to read the same way 100 times
primes := prime_sieve() for i := 0 ; i < 100 ; i++ { fmt.Println(time.Now(), <-primes)
|