v/examples/concurrency/concurrency.v

19 lines
493 B
V
Raw Normal View History

2020-06-17 02:34:16 +02:00
import time
// Simulate expensive computing using sleep function
fn expensive_computing(id int, duration int) {
println('Executing expensive computing task ($id)...')
time.wait(duration * time.millisecond)
println('Finish task $id on $duration ms')
2020-06-17 02:34:16 +02:00
}
fn main() {
mut threads := []thread{}
threads << go expensive_computing(1, 100)
threads << go expensive_computing(2, 500)
threads << go expensive_computing(3, 1000)
2020-06-17 02:34:16 +02:00
// Join all tasks
threads.wait()
2020-06-17 02:34:16 +02:00
println('All jobs finished!')
}