This is a creation in Article, where the information may have evolved or changed.
Directory
-
- Concurrent
- Goroutine
- Channel
- Overview
- Establish Channel
- Unidirectional Channel
- Use a Channel without a cache
- Use a cached Channel
- Multi-threaded synchronization
- Turn on multithreading
- Using Channel synchronization
- Synchronizing with Waitgroup
- Select
- Overview
- Using Select
- Set Timeout
Summary
Goroutine,channel effect, unidirectional and bidirectional channel, producer vs. consumer mode, cache with no cache Channel, synchronous vs. asynchronous, Waitgroup,select, timeout
Concurrent
Goroutine
Using Goroutine
Syntax forgo 方法名()
import"time"func test01() { fmt.Println("test01...")}go//test01...time.Sleep(1 * time.Second)
If you do not call the Sleep () method, the program ends after the main () method ends, and other threads do not get the chance to execute.
Channel
Overview
Channelis a reference type
ChannelCan be used to block threads
- You can set the cache, which defaults to
0
- The cache does not block until it fills up
ChannleThe primary role of message delivery and synchronization
Establish Channel
Grammar
- Create Channel
make(chan type, cacheSize)
- Close Channel
close(ch)
- Depositing data into the Channel
ch<-data
- Reading data from the Channel
<-ch
Unidirectional Channel
The channel that is created by default is bidirectional, and you can create read-only or write-only one-way channel.
Unidirectional Channel is typically used for message delivery.
Write only the Channel
varchanint
Read-only Channel
var ch2 <-chanint
Producer, consumer example
funcProducer (QueueChan<-int) { forI: =0; I <Ten; i++ {queue <-i fmt. Println ("Send:", i)}Close(queue)}funcConsumer (Queue <-Chan int) { forI: =0; I <Ten; i++ {V: = <-queue fmt. Println ("Receive:", v)}}queue: = Make(Chan int,1)GoProducer (queue)GoConsumer (queue) time. Sleep(1* Time. Second)
Use a Channel without a cache
There is no cache Channel , also known as synchronization Channel , that is used primarily for synchronization operations.
The receiver will block until there is data coming. If channel it is unbuffered, the sender will block until the receiver pulls the data out.
funcTEST02 (ChChan BOOL) {FMT. Println ("test02 ...") FMT. Println ("Waiting ...") Ch <-true Close(CH)} CH: = Make(Chan BOOL)GoTEST02 (CH)The ch blocking thread in the following code waits for input until true in test02 () is entered in the channel<-ch//test02 ... waiting ...CH = Make(Chan BOOL)GoTEST02 (CH) forV: =RangeCH {fmt. Println (v)}<-ch//test02...waiting...true
Use a cached Channel
The receiver will block until there is data coming. If there is a channel buffer, the sender blocks until the data is copied to the buffer, and if the buffer is full, the sender can recover from the blocking state only after the receiver has taken away the data.
func test05 (ch chan bool ) {fmt. Println ( "test05 ..." ) fmt. Println ( "Waiting test05 ..." ) ch <-true fmt. Println ( "Waiting ..." ) Fmt. Println ( "Waiting ..." ) Fmt. Println ( "Waiting ..." ) close (CH)} ch = make (chan BOOL , ten ) go test05 (ch) d: = <-chfmt. Println ( "D" , D) //test05 ... waiting test05 ... waiting01 Waiting02 waiting03 true
Multi-threaded synchronization
Turn on multithreading
Turn on multithreading using the following code
import"runtime"runtime.GOMAXPROCS(runtime.NumCPU())
Using Channel synchronization
functest03 (ChChan BOOL, indexint) {A: =1 forI: =0; I < +; i++ {A + = i} fmt. Println ("test03", index, a) ch <-true}ch = Make(Chan BOOL,Ten) forI: =0; I <Ten; i++ {Gotest03 (CH, i)//write 10 times to Channel} forI: =0; I <Ten; i++ {<-ch//Read 10 times from the Channel}
Synchronizing with Waitgroup
funcint) { a := 1 for i := 0; i < 1000; i++ { a += i } fmt.Println("test04", index, a) wg.Done()}wg := sync.WaitGroup{}wg.Add(10)for i := 0; i < 10; i++ { go test04(&wg, i)}wg.Wait()
Select
Overview
- can process multiple simultaneously in random order
channel
- Available
空 channel blocking main threads
- Time can be set
timout
Using Select
Definition of three Channel
make(chanintmake(chanstring//进行数据传输make(chanbool) //进行阻塞
Define Goroutine
Go func() { for{Select{ CaseV, OK: = <-c1:if!ok {o <-true Break} FMT. Println ("C1", V) CaseV, OK: = <-c2:if!ok {o <-true Break} FMT. Println ("C2", V)}}} () C1 <-1C2 <-"HI"C1 <-2C2 <-"Hello"Close(C1)Close(C2) <-o//c1 1, C2 Hi, C1 2, c2 hello
Set Timeout
select {case <-time.After(2 * time.Millisecond): fmt.Println("timeout")}