-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank.py
36 lines (31 loc) · 900 Bytes
/
bank.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
import threading
from time import sleep
from random import randint
class Account:
def __init__(self, balance):
self.balance = balance
class Transfer(threading.Thread):
def __init__(self, one, two):
super(Transfer, self).__init__()
self.one = one
self.two = two
self.daemon = True
def run(self):
while True:
self.transfer(randint(0, 10))
def transfer(self, amount):
self.one.balance -= amount
self.two.balance += amount
assert self.one.balance + self.two.balance == total, \
"One has %d, Two has %d, the total is %d" \
% (self.one.balance, self.two.balance, \
self.one.balance + self.two.balance)
alice = Account(500)
bob = Account(200)
total = 700
one = Transfer(alice, bob)
two = Transfer(bob, alice)
one.start()
two.start()
while True:
pass