Created
April 1, 2020 02:48
-
-
Save JesseYan/c137f8d572f8038b97e06a94c335bdb7 to your computer and use it in GitHub Desktop.
create pool using gorutines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
ch := make(chan int, 4) | |
go write(ch) | |
time.Sleep(2 * time.Second) | |
consume(ch) | |
} | |
func write(ch chan int) { | |
for i := 0; i < 16; i++ { | |
ch <- i | |
fmt.Println("successfully wrote", i, "to ch") | |
} | |
close(ch) | |
} | |
func consume(ch chan int) { | |
for c := range ch { | |
fmt.Println("consume:", c) | |
time.Sleep(1 * time.Second) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
create a pool for capacity 4
and produce 1 job and then consume 1, and so...
1.
produce
is not the same goroutine asconsume
2. Must close channel after
produce
; otherwise, consume will show error:print