From 209711b92311c6d14cc47da91f99a6e14956a72c Mon Sep 17 00:00:00 2001 From: haturatu Date: Fri, 13 Dec 2024 01:35:16 +0900 Subject: first commit --- README.md | 14 ++++++++++++++ gjl | Bin 0 -> 2137301 bytes go.mod | 3 +++ main.go | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 README.md create mode 100755 gjl create mode 100644 go.mod create mode 100644 main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..1dd7659 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# gjl +Goの並列処理をテストのために +[Go is not an easy language](https://www.arp242.net/go-easy.html) + +## Dev +```bash +go run main.go +``` +or +```bash +go build -o gjl main.go +./main.go +``` + diff --git a/gjl b/gjl new file mode 100755 index 0000000..c265f36 Binary files /dev/null and b/gjl differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e9106ff --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gjl + +go 1.23.4 diff --git a/main.go b/main.go new file mode 100644 index 0000000..ade5cce --- /dev/null +++ b/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "sync" + "time" +) + +func main() { + var ( + jobs = 20 // Run 20 jobs in total. + running = make(chan bool, 3) // Limit concurrent jobs to 3. + wg sync.WaitGroup // Keep track of which jobs are finished. + ) + + wg.Add(jobs) + for i := 1; i <= jobs; i++ { + running <- true // Fill running; this will block and wait if it's already full. + + // Start a job. + go func(i int) { + defer func() { + <-running // Drain running so new jobs can be added. + wg.Done() // Signal that this job is done. + }() + + // "do work" + time.Sleep(1 * time.Second) + fmt.Println(i) + }(i) + } + + wg.Wait() // Wait until all jobs are done. + fmt.Println("done") +} -- cgit v1.2.3