Junior — Middle
Milline on erinevus gorutina ja süsteemilõime vahel ülesannete täitmise kontekstis?
sobes.tech AI
Vastus AI-lt
A goroutine is a lightweight execution unit managed by the Go (runtime) environment, which uses very little memory and can be launched in large numbers simultaneously. A system thread is a heavier scheduling unit of the operating system.
Main differences:
- Goroutines are much lighter in resources than system threads.
- The scheduling of goroutines occurs within the Go runtime, not by the OS.
- Thousands of goroutines can operate simultaneously, while the number of system threads is usually smaller due to creation and switching costs.
Example of creating a goroutine:
func task() {
fmt.Println("Executing task")
}
func main() {
go task() // start goroutine
time.Sleep(time.Second) // wait for the goroutine to complete
}
Thus, goroutines enable efficient execution of many tasks in parallel with lower overhead.