Created
August 24, 2014 17:54
-
-
Save SteveBate/4e23f69d5939ad167135 to your computer and use it in GitHub Desktop.
Example showing use of Waitgroups and with the time.After channel. Remove the "go" call to have all calls to UpdateSchedulesReport run sequentially otherwise they run concurrently
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func main() { | |
var w sync.WaitGroup | |
w.Add(3) | |
go UpdateSchedulesReport(&w) | |
go UpdateSchedulesReport(&w) | |
go UpdateSchedulesReport(&w) | |
w.Wait() | |
fmt.Println("all over!") | |
} | |
func UpdateSchedulesReport(w *sync.WaitGroup) { | |
fmt.Println("waiting...") | |
ch := time.After(5 * time.Second) | |
for { | |
select { | |
case <-ch: | |
fmt.Println("time is ", time.Now()) | |
w.Done() | |
break | |
} | |
break | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment