python in 28
parent
a8edc611cd
commit
0bef5b997f
|
|
@ -0,0 +1,31 @@
|
|||
import threading
|
||||
import queue
|
||||
|
||||
class T(threading.Thread):
|
||||
def __init__(self, n, p, i, q):
|
||||
super().__init__()
|
||||
self.n = n
|
||||
self.p = p
|
||||
self.i = i
|
||||
self.q = q
|
||||
|
||||
def run(self):
|
||||
print(self.n, self.p, self.i, self.q)
|
||||
start = self.i * ((self.n // self.p))
|
||||
chunk = self.n // self.p + (0 if i < self.p-1 else self.n%self.p)
|
||||
self.q.put(sum([j for j in range(start, start+chunk)]))
|
||||
|
||||
Ts = []
|
||||
N = 100
|
||||
P = 3
|
||||
Q = queue.Queue()
|
||||
for i in range(P):
|
||||
Ts.append(T(N, P, i, Q))
|
||||
for t in Ts:
|
||||
t.start()
|
||||
ttl = 0
|
||||
for i in range(P):
|
||||
ttl += Q.get()
|
||||
for t in Ts:
|
||||
t.join()
|
||||
print(ttl)
|
||||
Loading…
Reference in New Issue