Junior — Middle
Millistel juhtudel asetatakse goroutinides olevad ülesanded täitmise planeerija ühisesse järjekorda?
sobes.tech AI
Vastus AI-lt
In Go, tasks (goroutines) are placed in the general execution scheduler's queue (run queue) in the following cases:
- When a goroutine is created with
goand is ready to execute, it is placed in the scheduler's queue. - If the goroutine gets blocked (for example, on I/O operations, channels, or synchronization) and then unblocked, it returns to the queue.
- When the current goroutine finishes, the scheduler selects the next one from the queue.
The Go scheduler uses an M:N model, where M are system threads and N are goroutines. Many goroutines exist, and they are distributed across threads via queues. The scheduler's queue is a structure where ready-to-run goroutines are stored.
Example:
func main() {
go func() {
fmt.Println("Goroutine started")
}()
time.Sleep(time.Second) // Wait for the goroutine to execute
}
Here, after creation, the goroutine is placed in the scheduler's queue for execution.