Go language three--parallel

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go supports parallel programming from a language level
Goroutine (The core of Go language parallel design), more efficient and easier than thread

Package Mainimport (        "FMT"        "runtime") Func say (Word string) {for        i:=0; i<10; i++ {//              runtime. Gosched ()       //yield time slices                fmt. PRINTLN (word)        }}func main () {        ///default system thread number is 1, can be adjusted by the following instruction, take full advantage of multi-core advantage        runtime. Gomaxprocs (4)        go Say ("Hello")        Say ("World")}

Multiple goroutine run in the same process, sharing memory data, but designed to follow: do not communicate through sharing, but share through communication

Go provides channel as data communication, similar to the UNIX pipeline, need to define the communication data type with make

package mainimport ("FMT" "runtime")//send function reads the channel without reading the data it sends to the channel, Func s End (name String,c Chan string) {w: = "Hello" + name//send message W to channel C c <-w/ /RECV Message from c w = <-c fmt. Printf ("A Got%v from recv\n", W)}func recv (C-chan string) {//RECV message from c w: = <-C FMT. Printf ("B Got%v from send\n", W)//send message to C c <-"OK."} Func Main () {runtime. Gomaxprocs (4) c: = Make (chan string) go Send ("B", c)//go recv (c) Res: = " "FMT. Scan (&res)}       

The

Channel receives and sends data is blocked, unless the other end is ready, which makes the Goroutines synchronization more simple without the need for explicit lock.

Buffered Channel
ch: = Make (chan type, value)
Value = = 0! unbuffered (blocked)
value > 0! buffering (non-blocking until value elements)

The producer closes the channel with the keyword close function, the channel is closed and no more data is sent, and the consumer can pass the syntax V, OK: = <-ch Test If the channel is closed. If OK returns false, then the channel has no data and has been closed.

Randomly selects a process between multiple channel via select

Package Mainimport ("FMT") func main () {chan_a: = Do (chan string) Chan_b: = Make (Chan string)                Chan_quit: = Make (Chan string) go func () {//Receive data from the Chan_a Channel five times and send the QUIT command For i:=0;i<5;i++ {fmt.                   Println ("Listen on Chan_a", <-chan_a)} chan_quit <-"Quit"} () Go func () {//Ibid. for i:=0;i<5;i++ {fmt.                   Println ("Listen on Chan_b", <-chan_b)} chan_quit <-"Quit"} () For {select {case chan_a <-"hello_a"://chan_a Channel writable FMT.P Rintln ("Send hello_a to Chan_a") Case Chan_b <-"Hello_b"://chan_b Channel writable FMT. Println ("Send Hello_b to Chan_b") case <-chan_quit://chan_quit channel readable FMT. Println("Quit") return//case <-time. After (5 * time.  Second)://Set timeout//default://Call default code when all channel is blocked}}}

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.