2020-06-17 03:34:16 +03:00
|
|
|
import time
|
|
|
|
|
|
|
|
// Simulate expensive computing using sleep function
|
2021-02-24 12:41:12 +03:00
|
|
|
fn expensive_computing(id int, duration int) {
|
2020-10-26 14:14:21 +03:00
|
|
|
println('Executing expensive computing task ($id)...')
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(duration * time.millisecond)
|
2020-10-26 14:14:21 +03:00
|
|
|
println('Finish task $id on $duration ms')
|
2020-06-17 03:34:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-02-24 12:41:12 +03:00
|
|
|
mut threads := []thread{}
|
2022-11-05 10:46:40 +03:00
|
|
|
threads << spawn expensive_computing(1, 100)
|
|
|
|
threads << spawn expensive_computing(2, 500)
|
|
|
|
threads << spawn expensive_computing(3, 1000)
|
2020-06-17 03:34:16 +03:00
|
|
|
// Join all tasks
|
2021-02-24 12:41:12 +03:00
|
|
|
threads.wait()
|
2020-06-17 03:34:16 +03:00
|
|
|
println('All jobs finished!')
|
|
|
|
}
|