Using Go's goroutine and channel to achieve a simple "production, consumption" (with time-out control) Small example

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

Directly on the code

1, no time-outs

 PackageMainImport("FMT"    "Time")funcMain () {//Storage of production channelJobchan: = Make(Chan int, -)//Notify if all tasks are completedEndchan: = Make(Chan BOOL)GoProduction (Jobchan)GoWorker (Jobchan, Endchan)Select{ Case<-endchan:fmt. Println ("The consumption is done ......... .....")return     Case<-time. After (time. Second * -): FMT. Println ("Time-out ...... ..........")return}}//ConsumptionfuncWorker (Jobchan <-Chan int, EndchanChan BOOL) { forJob: =RangeJobchan {fmt. Println ("Consumption:", Job)}//end of consumption, notify EndchanEndchan <-true}//ProductionfuncProduction (JobchanChan<-int) { forI: =1; I <=Ten; i++ {fmt. Println ("Production:", i) Jobchan <-i}//Close channel to prevent consumer congestion    Close(Jobchan)}

Execution Result:

Visible production and consumption asynchronous concurrent execution.

2, when timeout (timeout setting dot, and increase consumption processing time)

 PackageMainImport("FMT"    "Time")funcMain () {//Storage of production channelJobchan: = Make(Chan int, -)//Notify if all tasks are completedEndchan: = Make(Chan BOOL)GoProduction (Jobchan)GoWorker (Jobchan, Endchan)Select{ Case<-endchan:fmt. Println ("The consumption is done ......... .....")return     Case<-time. After (time. Second *5): FMT. Println ("Time-out ...... ..........")return}}//ConsumptionfuncWorker (Jobchan <-Chan int, EndchanChan BOOL) { forJob: =RangeJobchan {fmt. Println ("Consumption:", job)//Increase consumption processing timeTime. Sleep(1* Time. Second)}//end of consumption, notify EndchanEndchan <-true}//ProductionfuncProduction (JobchanChan<-int) { forI: =1; I <=Ten; i++ {fmt. Println ("Production:", i) Jobchan <-i}//Close channel to prevent consumer congestion    Close(Jobchan)}

Execution results

Timeout after 5 seconds (there is still data in the queue that has not been processed).

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.