Concurrent concurrency of Go_11:go Language Foundation

Source: Internet
Author: User

Concurrent concurrency

Many people are directed at Go hype high concurrency and can not help but, in fact, from the source of analysis, Goroutine is only by the official implementation of the Super "thread pool" just. But then again, the stack memory footprint of each instance 4~5kb and the significant reduction in creation and destruction overhead due to the implementation mechanism are the root causes of the high concurrency that Go claims to be made. In addition, Goroutine's ease of use also gives developers a huge traversal at the language level.

High concurrency must be noted: concurrency is not parallel.

Concurrency is mainly by switching time slices to achieve "simultaneous" operation, while parallel is the direct use of multi-core implementation of multi-threaded operation, but Go can set the use of the number of cores to play a multi-core computer processing capacity.

Goroutine pursues communication to share memory, rather than shared memory. The go language is mainly through Channe technology communication to achieve the sharing of memory, because the channel is a tunnel, Go is through the channel to communicate the sharing of memory data.

Let us first look at one of the simplest goroutine cases:

Package Mainimport (    "fmt"    "time") Func Main () {    // enable a goroutine    go GoRun ()    / / Adding a sleep here is because the main thread has started to die, and the child thread is too late to execute time    . Sleep (2 * Time . Second)}func GoRun () {    fmt. Println ("go Go Go!!! " )}

Operation Result:

Go Go Go!!!

Channel

1. Channel is the bridge of goroutine communication, mostly blocking synchronous

2. It is created by make, close closes

3. Channel is a reference type

4. You can use the for range to iterate and continue to operate the channel

5. One-way or two-way channel can be set

6. You can set the cache size so that it does not block until it is filled, that is, it is asynchronous

So for the upstream code we don't use hibernation, and we use the Channel to achieve the effect we want:

Package Mainimport ("FMT") Func main () {//declaration creates a channel with a storage type of bool typec: = Make (chanBOOL)    //enable a goroutine, using anonymous methodsgo func () {fmt. Println ("Go Go Go!!!") C<-true  //to deposit a value into the channel    }()    //when the program finishes executing, the value that you just assigned is removed from the channel<-C/** The main thread starts an anonymous child thread and executes to: <-C, the main thread is blocked when it arrives here. The main thread blocks only when a child thread puts a value into the channel. Actually, this is the completion of the message sent*/}

The upstream code can be modified to use a for range to send messages:

Package Mainimport ("FMT") Func main () {//declaration creates a channel, the storage type is bool, the channel set here is two-way channels , both can be saved or can be takenc: = Make (chanBOOL)    //enable a goroutine, using anonymous methodsgo func () {fmt. Println ("Go Go Go!!!") C<-true  //to deposit a value into the channelClose (c) //Remember that if you use a for range to take a value, you need to close it somewhere else, or a deadlock will occur } ()    //Loop through the channel to remove the value just assigned     forV: =range C {fmt. Println (v)} }

From the above code can be seen, the general use of channels are two-way channel, namely: can be taken and can be saved. What is the one-way channel commonly used in the scenario?

One-way channel is divided into two types, one is only readable, one is only stored, generally used for parameter type transfer. For example, there is a method to return a channel type, the general requirements of the operation can only be taken from here, then its purpose is only to store the type, if you do not care to save data at this time, the occurrence of panic causes the program to crash an exception. Then read the type of channel similarly. In fact, this is also for the security and robustness of the program, to prevent some mis-operation.

Here's another point of knowledge, the difference between a cached channel and a non-cached channel?

Make (chan bool, 1) indicates that a cache Channel;make (chan bool) with a cache size represents a non-cached channel

The non-cached channel is blocking that is synchronous, while the cache channel is asynchronous.

What do you say? Like what

C1:=make (chan int) unbuffered

C2:=make (Chan int,1) has a cushion

C1<-1

Unbuffered not only to the C1 Channel 1 but always have to have other Ctrip <-C1 took over this parameter, then c1<-1 will continue, otherwise it has been blocked

The c2<-1 is not blocked, because the buffer size is 1 only when the second value is put in the first one has not been taken away, this time will be blocked.

Make a metaphor.

No buffer is a messenger to your door to send letters, you are not at home he does not go, you must take the letter, he will go.

No buffer guarantee to get your hands on the letter.

There is a buffer is a messenger to your home still to your home mailbox turned away, unless your mailbox is full he must wait for the mailbox empty.

There is a buffer guarantee to enter your home mailbox.

Concurrent concurrency of Go_11:go Language Foundation

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.