Add: very simple example of a multi-threaded program
This commit is contained in:
parent
4c1e355303
commit
6905e99be2
1 changed files with 25 additions and 0 deletions
25
basic-multi-threaded.go
Normal file
25
basic-multi-threaded.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Function that prints numbers from 1 to 5 with a delay
|
||||
func printNumbers(id int) {
|
||||
for i := 1; i <= 5; i++ {
|
||||
fmt.Printf("Goroutine %d: %d\n", id, i)
|
||||
time.Sleep(100 * time.Millisecond) // Delay for visibility
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Launch 3 goroutines
|
||||
for i := 1; i <= 3; i++ {
|
||||
go printNumbers(i)
|
||||
}
|
||||
|
||||
// Wait for goroutines to finish
|
||||
time.Sleep(1 * time.Second) // Ensure main does not exit immediately
|
||||
fmt.Println("All goroutines finished")
|
||||
}
|
Loading…
Reference in a new issue