Created
April 20, 2023 03:43
-
-
Save agambondan/63cc7c644685ae779d9395ec7f0a400a to your computer and use it in GitHub Desktop.
This is Example Every Type Concurrency in Go
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" | |
"sync" | |
"time" | |
) | |
// Goroutines | |
func goroutine1() { | |
fmt.Println("goroutine 1") | |
} | |
func goroutine2() { | |
fmt.Println("goroutine 2") | |
} | |
// Channels | |
func worker(id int, jobs <-chan int, results chan<- int) { | |
for j := range jobs { | |
fmt.Printf("worker %d processing job %d\n", id, j) | |
time.Sleep(time.Second) | |
results <- j * 2 | |
} | |
} | |
// WaitGroups | |
func worker2(id int, wg *sync.WaitGroup) { | |
defer wg.Done() | |
fmt.Printf("worker %d starting\n", id) | |
time.Sleep(time.Second) | |
fmt.Printf("worker %d done\n", id) | |
} | |
// Mutexes | |
type Counter struct { | |
value int | |
mu sync.Mutex | |
} | |
func (c *Counter) Increment() { | |
c.mu.Lock() | |
defer c.mu.Unlock() | |
c.value++ | |
} | |
func (c *Counter) Value() int { | |
c.mu.Lock() | |
defer c.mu.Unlock() | |
return c.value | |
} | |
// RWMutexes | |
type Cache struct { | |
value int | |
mu sync.RWMutex | |
} | |
func (c *Cache) Read() int { | |
c.mu.RLock() | |
defer c.mu.RUnlock() | |
return c.value | |
} | |
func (c *Cache) Write(value int) { | |
c.mu.Lock() | |
defer c.mu.Unlock() | |
c.value = value | |
} | |
// Select statements | |
func server1(ch chan<- string) { | |
time.Sleep(time.Second) | |
ch <- "server1" | |
} | |
func server2(ch chan<- string) { | |
time.Sleep(time.Second) | |
ch <- "server2" | |
} | |
func main() { | |
// Goroutines | |
go goroutine1() | |
go goroutine2() | |
time.Sleep(time.Second) | |
// Channels | |
jobs := make(chan int, 100) | |
results := make(chan int, 100) | |
for w := 1; w <= 3; w++ { | |
go worker(w, jobs, results) | |
} | |
for j := 1; j <= 5; j++ { | |
jobs <- j | |
} | |
close(jobs) | |
for a := 1; a <= 5; a++ { | |
fmt.Println(<-results) | |
} | |
// WaitGroups | |
var wg sync.WaitGroup | |
for i := 1; i <= 5; i++ { | |
wg.Add(1) | |
go worker2(i, &wg) | |
} | |
wg.Wait() | |
// Mutexes | |
var counter Counter | |
var wg2 sync.WaitGroup | |
for i := 0; i < 1000; i++ { | |
wg2.Add(1) | |
go func() { | |
defer wg2.Done() | |
counter.Increment() | |
}() | |
} | |
wg2.Wait() | |
fmt.Println(counter.Value()) | |
// RWMutexes | |
var cache Cache | |
go func() { | |
for i := 0; i < 10; i++ { | |
cache.Write(i) | |
time.Sleep(time.Millisecond * 100) | |
} | |
}() | |
for i := 0; i < 5; i++ { | |
go func() { | |
for { | |
fmt.Println(cache.Read()) | |
time.Sleep(time.Millisecond * 50) | |
} | |
}() | |
} | |
time.Sleep(time.Second) | |
// Select statements | |
ch1 := make(chan string) | |
ch2 := make(chan string) | |
go server1(ch1) | |
go server2(ch2) | |
select { | |
case s1 := <-ch1: | |
fmt.Println(s1) | |
case s2 := <-ch2: | |
fmt.Println(s2) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment