Junior — Middle
Can you explain the concept of pseudo-parallel processes in programming?
sobes.tech AI
Answer from AI
Pseudo-parallel processes are a way of executing multiple tasks that appear to run simultaneously, but on the processor, they are executed sequentially. In programming, this is achieved with a task scheduler that quickly switches between processes or threads, creating the illusion of parallelism.
In Go, this is implemented through goroutines — lightweight threads that the Go scheduler runs on one or more system threads. Goroutines can execute pseudo-parallelly, especially if you have a single processor, where tasks switch very quickly.
Example in Go:
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("Hello") // start a goroutine
say("World") // main goroutine
}
Here, two functions execute almost simultaneously, but in reality, Go switches between them quickly, creating the effect of pseudo-parallelism.