From 5d35abc39d810e0d28b96ef90e74f2112c1a9664 Mon Sep 17 00:00:00 2001 From: Jian Han Date: Sun, 3 Dec 2017 15:50:40 +1000 Subject: [PATCH] added sem simple worker pool --- concurrency/simplesem/main.go | 25 +++++++++++++++++++++++++ urfavecli/main.go | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 concurrency/simplesem/main.go create mode 100644 urfavecli/main.go diff --git a/concurrency/simplesem/main.go b/concurrency/simplesem/main.go new file mode 100644 index 0000000..8db0d50 --- /dev/null +++ b/concurrency/simplesem/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + concurrentCount := 5 + sem := make(chan bool, concurrentCount) + + for i := 0; i < 100; i++ { + sem <- true + go func(i int) { + dummyProcess(i) + <-sem + }(i) + } + time.Sleep(time.Second * 20) +} + +func dummyProcess(i int) { + fmt.Printf("Process Dummy %v \n", i) + time.Sleep(time.Second) +} diff --git a/urfavecli/main.go b/urfavecli/main.go new file mode 100644 index 0000000..1e7a9b6 --- /dev/null +++ b/urfavecli/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "fmt" + "os" + + "github.com/urfave/cli" +) + +func main() { + app := cli.NewApp() + app.Name = "hello_cli" + app.Usage = "Print hello world" + app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "name, n", + Value: "World", + Usage: "Who to say hello to.", + }, + } + app.Action = func(c *cli.Context) error { + name := c.GlobalString("name") + fmt.Printf("Hello %s!\n", name) + return nil + } + app.Run(os.Args) +}