-
Notifications
You must be signed in to change notification settings - Fork 52
/
mtfacfib.py
51 lines (37 loc) · 949 Bytes
/
mtfacfib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
from myThread import MyThread
from time import ctime, sleep
def fib(x):
sleep(0.005)
if x < 2: return 1
return fib(x-2) + fib(x-1)
def fac(x):
sleep(0.1)
if x < 2: return 1
return x * fac(x-1)
def sum(x):
sleep(0.1)
if x < 2: return 1
return x + sum(x-1)
funcs = [fib, fac, sum]
n = 12
def main():
nfuncs = range(len(funcs))
print("*** SINGLE THREAD")
for i in nfuncs:
print("starting", funcs[i].__name__, 'at:', ctime())
print(funcs[i](n))
print(funcs[i].__name__, "finished at:", ctime())
print("\n*** MULTIPLE THREADS")
threads = []
for i in nfuncs:
t = MyThread(funcs[i], (n,), funcs[i].__name__)
threads.append(t)
for i in nfuncs:
threads[i].start()
for i in nfuncs:
threads[i].join()
print(threads[i].getResult())
print("all DONE")
if __name__ == "__main__":
main()