awesome-patterns/ardan_labs/thread_pooling/mian.go
2018-03-01 16:05:15 +10:00

65 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
// article https://www.ardanlabs.com/blog/2013/05/thread-pooling-in-go-programming.html
import (
"bufio"
"fmt"
"os"
"runtime"
"strconv"
"time"
)
type MyWork struct {
Name string
BirthYear int
WP *WorkPool
}
func (mw *MyWork) DoWork(workRoutine int) {
fmt.Printf("%s : %d\n", mw.Name, mw.BirthYear)
fmt.Printf("Q:%d R:%d\n", mw.WP.QueuedWork(), mw.WP.ActiveRoutines())
// Simulate some delay
time.Sleep(100 * time.Millisecond)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
// we create a thread pool where the number of routines to use is based on the number of cores we have on the machine.
// This means we have a routine for each core. You cant do any more work if each core is busy.
// Again, performance testing will determine what this number should be. The second parameter is the size of the queue.
// In this case I have made the queue large enough to handle all the requests coming in.
workPool := New(runtime.NumCPU(), 800)
shutdown := false // Race Condition, Sorry
go func() {
for i := 0; i < 1000; i++ {
work := MyWork{
Name: "A" + strconv.Itoa(i),
BirthYear: i,
WP: workPool,
}
if err := workPool.PostWork("routine", &work); err != nil {
fmt.Printf("ERROR: %s\n", err)
time.Sleep(100 * time.Millisecond)
}
if shutdown == true {
return
}
}
}()
fmt.Println("Hit any key to exit")
reader := bufio.NewReader(os.Stdin)
reader.ReadString('\n')
shutdown = true
fmt.Println("Shutting Down")
workPool.Shutdown("routine")
}