Last active
May 27, 2018 05:33
-
-
Save hauxe/21bc9efbb9b082224d9f66f63cd6572b to your computer and use it in GitHub Desktop.
Worker pool
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 | |
// Job define job interface | |
type Job interface { | |
Execute() | |
} | |
// JobQueue queue of jobs | |
var JobQueue chan Job | |
// WorkerNum number of workers | |
var WorkerNum int | |
func main() { | |
WorkerNum = 100 | |
JobQueue := make(chan Job, WorkerNum) | |
for i := 0; i < WorkerNum; i++ { | |
// start a worker | |
go func() { | |
for { | |
job := <-JobQueue | |
// do work | |
job.Execute() | |
} | |
}() | |
} | |
// wait for jobs | |
for { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment