Created
April 27, 2012 16:33
-
-
Save angeldm/2510620 to your computer and use it in GitHub Desktop.
Worker
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" | |
) | |
var work = 5 | |
type Request struct { | |
fn func() int | |
c chan int | |
} | |
// Simulation of some work: just sleep for a while and report how long. | |
func op() int { | |
n := 5 * time.Second | |
time.Sleep(time.Duration(n)) | |
fmt.Println("Done.") | |
return int(n) | |
} | |
func do(c,e chan int) { | |
n := op() | |
c <- n | |
work-- | |
} | |
func doWork() { | |
c := make(chan int, 10) | |
e := make(chan int) | |
for i := 0; i < work; i++ { | |
go do(c,e) | |
//op() | |
} | |
//work=0 | |
for work!=0 { | |
select { | |
case <-c : | |
fmt.Println(work) | |
case <-e: | |
fmt.Println("Finish") | |
} | |
} | |
} | |
func main() { | |
fmt.Println("Go !") | |
doWork() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment