summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhaturatu <taro@eyes4you.org>2024-12-13 01:35:16 +0900
committerhaturatu <taro@eyes4you.org>2024-12-13 01:35:16 +0900
commit209711b92311c6d14cc47da91f99a6e14956a72c (patch)
treedf77700e6913236a74d22cb944712f78559915c9
first commit
-rw-r--r--README.md14
-rwxr-xr-xgjlbin0 -> 2137301 bytes
-rw-r--r--go.mod3
-rw-r--r--main.go35
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
+```
+
diff --git a/gjl b/gjl
new file mode 100755
index 0000000..c265f36
--- /dev/null
+++ b/gjl
Binary files 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")
+}