v2 / examples / concurrency / concurrency.v
18 lines · 16 sloc · 509 bytes · 017ace6ea7402430a992aa0820d5e472ebca74c7
Raw
1import time
2
3// Simulate expensive computing using sleep function
4fn expensive_computing(id int, duration int) {
5 println('Executing expensive computing task (${id})...')
6 time.sleep(duration * time.millisecond)
7 println('Finish task ${id} on ${duration} ms')
8}
9
10fn main() {
11 mut threads := []thread{}
12 threads << spawn expensive_computing(1, 100)
13 threads << spawn expensive_computing(2, 500)
14 threads << spawn expensive_computing(3, 1000)
15 // Join all tasks
16 threads.wait()
17 println('All jobs finished!')
18}
19