File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed
Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change 1+ import argparse
2+ import threading
3+ import time
4+
5+
6+ # Calculate the factorial
7+ def factorial (n , t ):
8+ time .sleep (n * t / 4 )
9+ print ("Start: " + str (t ) + ": " + str (n ))
10+ if n == 1 :
11+ res = 1
12+ if t == 1 :
13+ print ("Feel free to break here" )
14+ else :
15+ res = n * factorial (n - 1 , t )
16+ return res
17+
18+
19+ # Calculate the factorial and print the result
20+ def factorial_thread (n , t ):
21+ time .sleep (2 )
22+ result = factorial (n , t )
23+ print ("Thread " + str (t ) + " = " + str (result ))
24+
25+
26+ def launch_factorials (n ):
27+ threads = []
28+ print ("Calculate: " + str (n ))
29+ breakpoint ()
30+ for i in range (n ):
31+ threads .append (threading .Thread (target = factorial_thread , args = (n + i , i + 1 )))
32+ threads [- 1 ].start ()
33+ print ("Wait for the results" )
34+ for thread in threads :
35+ thread .join ()
36+ print ("Done" )
You can’t perform that action at this time.
0 commit comments