v / examples / concurrency
Raw file | 18 loc (16 sloc) | 509 bytes | Latest commit hash 017ace6ea
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}