goroutines Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/goroutines/ Tue, 25 Feb 2020 20:16:07 +0000 en-US hourly 1 https://wordpress.org/?v=6.8.1 https://i0.wp.com/golangbyexamples.com/wp-content/uploads/2021/05/cropped-go_border-1.png?fit=32%2C32&ssl=1 goroutines Archives - Welcome To Golang By Example https://vikasboss.github.io/tag/goroutines/ 32 32 159787465 Wait for all Go routines to finish execution in Golang https://vikasboss.github.io/wait-all-goroutines-go/ https://vikasboss.github.io/wait-all-goroutines-go/#respond Tue, 25 Feb 2020 11:24:57 +0000 https://vikasboss.github.io/?p=1490 sync package of golang provides WaitGroup struct which can be used to wait for collection of goroutines to finish execution. The WaitGroup provides: Add method to set the number of goroutines to...

The post Wait for all Go routines to finish execution in Golang appeared first on Welcome To Golang By Example.

]]>
sync package of golang provides WaitGroup struct which can be used to wait for collection of goroutines to finish execution. The WaitGroup provides:

  • Add method to set the number of goroutines to wait for.
  • Done method which is called by each of the goroutines when finished.
  • Wait method which is used to block till all goroutines have finished and have called the Done method

Let’s see a working code

package main

import (
    "fmt"
    "sync"
    "time"
)

func main() {
    var wg sync.WaitGroup
    wg.Add(2)
 
    go sleep(&wg, time.Second*1)
    go sleep(&wg, time.Second*2)
 
    wg.Wait()
    fmt.Println("All goroutines finished")
}

func sleep(wg *sync.WaitGroup, t time.Duration) {
    defer wg.Done()
    time.Sleep(t)
    fmt.Println("Finished Execution")
}

Output:

Finished Execution
Finished Execution
All goroutines finished

The post Wait for all Go routines to finish execution in Golang appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/wait-all-goroutines-go/feed/ 0 1490
Get number of currently running/active goroutines https://vikasboss.github.io/number-currently-running-active-goroutines/ https://vikasboss.github.io/number-currently-running-active-goroutines/#respond Sun, 23 Feb 2020 06:29:57 +0000 https://vikasboss.github.io/?p=1437 NumGoroutine function of runtime package can be used to know the currently running/active goroutines. https://golang.org/pkg/runtime/#NumGoroutine Below is the signature of the function Working Code: Output:

The post Get number of currently running/active goroutines appeared first on Welcome To Golang By Example.

]]>
NumGoroutine function of runtime package can be used to know the currently running/active goroutines.

https://golang.org/pkg/runtime/#NumGoroutine

Below is the signature of the function

func NumGoroutine() int

Working Code:

package main

import (
    "fmt"
    "runtime"
    "time"
)

func main() {
    for i := 0; i < 20; i++ {
        go execute()
    }
    fmt.Println(runtime.NumGoroutine())
}

func execute() {
    time.Sleep(time.Second * 10)
}

Output:

21

The post Get number of currently running/active goroutines appeared first on Welcome To Golang By Example.

]]>
https://vikasboss.github.io/number-currently-running-active-goroutines/feed/ 0 1437