Fragment Description:



Channel ordering Using time.Sleep() Attention to durations of sleep, respect the order!


channelTimeOrdering

Go Playground

Last update, on 2015, Fri 9 Oct, 16:15:28

/* ... <== see fragment description ... */

package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("In main()")
    go longWait()
    go shortWait()
    fmt.Println("About to sleep in main()")
    // long enough to let the 2 goroutine finish their sleep
    time.Sleep(10 * 1e9)
    fmt.Println("End main()")
}
func longWait() {
    fmt.Println("Beginning longWait()")
    // long sleep
    time.Sleep(5 * 1e9)
    fmt.Println("Exiting longWait()")
}
func shortWait() {
    fmt.Println("Beginning shortWait()")
    // shot sleep
    time.Sleep(2 * 1e9)
    fmt.Println("Exiting shortWait()")
}

/* Expected Output:
In main()
About to sleep in main() ...
Beginning longWait() ...
Beginning shortWait() ...
Exiting shortWait()
Exiting longWait()
End main()
*/



Comments