Brother Lian Blockchain training share go language concurrency timeout processing

Source: Internet
Author: User

In the first quarter of 2018, the demand for blockchain-related talent had reached 9.7 times times the same period in 2017, with a 4.6 times-fold increase in the number of companies publishing blockchain-related posts.


Brother Lian Education go all-in-one and Blockchain training courses are unique, structured courses developed by Tsinghua, Microsoft, and Google teachers over the course of a half-year period.


Golang does not provide time-outs at the language level, but it can be timed out with some small tricks.

Principle:

Concurrent a function, waiting for 1s to write data to timeout, in the Select if there is data in the 1s to the other channel write will be executed smoothly, if not, this is timeout write data, we know the timeout.

Package Main

Import "FMT"

Import "Time"


Func Main () {

CH: = Make (chan int, 1)

Timeout: = Make (chan bool, 1)


Executes a function concurrently, waiting for 1s to write to timeout to true

Go func () {

Time. Sleep (1000)

Timeout <-True

}()



This will wait for CH or timeout to read the data

Because no data has been written to Ch

Data is written to timeout after 1s

So the case of timeout was executed.

Use this technique to implement a timeout operation

Select {

Case <-Ch:

Fmt. Println ("read from ch")

Case <-Timeout:

Fmt. Println ("Time Out ...")

}

}

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.