summaryrefslogtreecommitdiff
path: root/main.go
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 /main.go
first commit
Diffstat (limited to 'main.go')
-rw-r--r--main.go35
1 files changed, 35 insertions, 0 deletions
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")
+}