Golang Concurrent Programming Basics

Source: Internet
Author: User
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() {    fori := 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      }

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 stringfmt.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 ,可以接受一个stringdata<-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() chanint {ch := make(chanint)go func() {fori := 2; ; i++ {ch &lt;- i}}()returnch}//素数筛选器func prime_filter(in chanint, primeint) chanint {out := make(chanint)go func() {for{ifi := &lt;-in; i%prime != 0{out &lt;- i}}}()returnout}//素数消费者func prime_sieve() chanint {out := make(chanint)go func() {ch := prime_generate()for{prime := &lt;-chout &lt;- primech = prime_filter(ch, prime)}}()returnout}

This is OK, the main function is to read the same way 100 times

primes := prime_sieve()    fori := 0; i &lt;100; i++ {        fmt.Println(time.Now(), &lt;-primes)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.