Created
March 15, 2012 00:24
-
-
Save hsaito/2040677 to your computer and use it in GitHub Desktop.
Random test code to figure out concurrency on Go language
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" | |
"time" | |
) | |
func main() { | |
c1 := make(chan bool) | |
go func() { | |
for i := 0; i < 10; i++ { | |
fmt.Printf(fmt.Sprintf("First: %d\n",i)) | |
time.Sleep(1000000000) | |
} | |
c1 <- true; | |
fmt.Println("First is complete") | |
} () | |
c2 := make(chan bool) | |
go func() { | |
for i := 0; i < 10; i++ { | |
fmt.Printf(fmt.Sprintf("Second: %d\n",i)) | |
time.Sleep(2000000000) | |
} | |
c2 <- true; | |
fmt.Println("Second is complete") | |
} () | |
fmt.Println("Waiting for functions to complete") | |
<-c1 | |
<-c2 | |
fmt.Println("Done") | |
} | |
/* | |
Output: | |
Waiting for functions to complete | |
First: 0 | |
Second: 0 | |
First: 1 | |
Second: 1 | |
First: 2 | |
First: 3 | |
Second: 2 | |
First: 4 | |
First: 5 | |
Second: 3 | |
First: 6 | |
First: 7 | |
Second: 4 | |
First: 8 | |
First: 9 | |
Second: 5 | |
First is complete | |
Second: 6 | |
Second: 7 | |
Second: 8 | |
Second: 9 | |
Second is complete | |
Done | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment