diff options
author | haturatu <taro@eyes4you.org> | 2024-12-13 01:35:16 +0900 |
---|---|---|
committer | haturatu <taro@eyes4you.org> | 2024-12-13 01:35:16 +0900 |
commit | 209711b92311c6d14cc47da91f99a6e14956a72c (patch) | |
tree | df77700e6913236a74d22cb944712f78559915c9 |
first commit
-rw-r--r-- | README.md | 14 | ||||
-rwxr-xr-x | gjl | bin | 0 -> 2137301 bytes | |||
-rw-r--r-- | go.mod | 3 | ||||
-rw-r--r-- | main.go | 35 |
4 files changed, 52 insertions, 0 deletions
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 +``` + Binary files differ@@ -0,0 +1,3 @@ +module gjl + +go 1.23.4 @@ -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") +} |