Created
May 29, 2011 10:17
-
-
Save tkawachi/997627 to your computer and use it in GitHub Desktop.
goroutine test on app engine
This file contains hidden or 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 hello | |
import ( | |
"fmt" | |
"http" | |
"time" | |
) | |
func init() { | |
http.HandleFunc("/", handler) | |
} | |
func goroutine(n int, w http.ResponseWriter, ch chan int) { | |
for i := 0; i < 5; i++ { | |
fmt.Fprintf(w, "Routine %d: %d\n", n, i) | |
time.Sleep(1000) // sleep 1ms | |
} | |
ch <- 0 | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "text/plain") | |
ch := make(chan int) | |
for i := 0; i < 3; i++ { | |
go goroutine(i, w, ch) | |
} | |
for i := 0; i < 3; i++ { | |
<-ch | |
} | |
fmt.Fprintf(w, "Done.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment