1 | import time |
2 | |
3 | // Simulate expensive computing using sleep function |
4 | fn 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 | |
10 | fn 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 | } |