Go Language Learning Note 2/2

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. CHAP04 Concurrent Programming
The 1.Go language supports language-level lightweight threads, called Goroutine, that are managed by the go language runtime runtime. The following multithreaded instances:
Package Main
Import (
"FMT";
"Sync";
"Runtime"
)
var counter int = 0
Func Count (lock *sync. Mutex) {
Lock. Lock ()
counter++
Fmt. Println (counter)
Lock. Unlock ()
}


Func Main () {
Lock:=&sync. mutex{}
For i:=0;i<10;i++ {
Go Count (Lock)
}


for {
Lock. Lock ()
C:=counter
Lock. Unlock ()
Runtime. Gosched ()//For the CPU to make time slices
If c>=10 {
Break
}
}
}
2.Go language shared data concept: Do not communicate through shared memory, but should share memory through communication.
3.channel is the way of communication between the go language goroutine. You can use the channel to pass messages between different goroutine threads. Channel is type-dependent.
Definition: var channame chan type, initialize: channame: = Make (chan type).
Write: Channame <-value, read: Value: = <-channame. Reading can cause the program to block waiting.
Package Main
Import "FMT"
Import "Time"
Func Count (ch Chan int) {
CH <-1
Fmt. Println (time. Now (). Unixnano ())
}


Func Main () {
CHS: = Make ([]chan int,10)
For i:=0;i<10;i++{
Chs[i] = make (chan int)
Go Count (Chs[i])
}


For _,ch: = Range (CHS) {
<-ch
Fmt. Println ("--------")
}
}
Output:
1470927062503429479
--------
4.select is used to process asynchronous IO, similar to switch, but the condition of case must be IO operation without judging condition. Select executes a running case randomly. If there is no case to run, it will block until there is one to run.
Package Main
Import "FMT"
Func Main () {
CH: = Make (chan int,1)
for {
Select {
Case CH <-0:
Case CH <-1:
}
I:=<-ch
Fmt. Println ("Value received", I)
}
}
You need to ensure that there is a case to execute, otherwise deadlock: Fatal Error:all Goroutines is asleep-deadlock!
Package Main
Import "FMT"


Func Main () {
CH: = Make (chan int,1)
for {
Select {
Case &LT;-CH:
}
Fmt. Println ("Value received")
}
}
5. Buffering mechanism: Set buffer, the buffer is full before blocking write. Ch:=make (Chan int,1024)
6. Use channel processing timeout:
Package Main
Import "FMT"
Import "Time"
Func Main () {
Timeout: = Make (chan bool,1)
Go func () {
Time. Sleep (time. Second)
Timeout<-true
}()
var ch Chan int
Select {
Case <-Ch:
Case <-Timeout:
Fmt. Println ("TimeOut ...")
}
}
7. Single channel:
Normal: var ch chan int
Single write: var ch chan<-int
Single read: var ch <-chan int
8.runtime. Gomaxprocs (n) sets n CPUs to participate in the operation. Runtime. Sched () Releases the time slice.
9. Sync Lock: Syncmutex. Read/write Lock: Sync. Rwmutex.
10. Globally unique operation: sync. Once
Package Main
Import "FMT"
Import "Sync"
var once sync. Once
Func hi () {
Fmt. Print ("HI")
}
Func Main () {
For i:=0;i<10;i++ {
Fmt. Println ("Round", i)
Once. Do (HI)
}


}
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.