Go Language _ Asynchronous Programming _example.01

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

Package Main


Import (
"Log"
"Math/rand"
"Time"

)


Func uploadnetvaluefile (done chan bool) {
Using random functions to simulate processing time for different files
r: = Rand. New (Rand. Newsource (time. Now (). Unixnano ()))
x: = R.INTN (20)
Log. Println ("Uploadnetvaluefile:", x)
Time. Sleep (time. Second * time. Duration (x))
Log. Println ("Uploadnetvaluefile OK")
Done <-True

}


Func uplaodsalefarefile (done chan bool) {
Using random functions to simulate processing time for different files
r: = Rand. New (Rand. Newsource (time. Now (). Unixnano () + 10))
x: = R.INTN (20)
Log. Println ("Uplaodsalefarefile:", x)
Time. Sleep (time. Second * time. Duration (x))
Log. Println ("Uplaodsalefarefile OK")
Done <-True

}


Func Main () {
T1: = time. Now ()
Tasknum: = 2

Done: = Make (chan bool, tasknum)


//Simulate when the file was generated
Time. Sleep (time. Second * 1)

Log. Println ("Create Netvalue file!")


//Concurrent execution

Go Uploadnetvaluefile (done)


//Simulate when the file was generated
Time. Sleep (time. Second * 1)

Log. Println ("Create Salefare file!")


//Concurrent execution

Go Uplaodsalefarefile (done)


//wait for all concurrent finishes
Func () {
For I: = 0; i < Tasknum; i++ {
Log. Println (<-done)
}

}()


t2: = time. Now ()
Log. Printf ("Time-consuming:%d\n", T2. Sub (t1))
}


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.