-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallelOperation.py
More file actions
63 lines (49 loc) · 1.29 KB
/
parallelOperation.py
File metadata and controls
63 lines (49 loc) · 1.29 KB
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
52
53
54
55
56
57
58
59
60
61
import threading
from threading import Thread
from time import sleep
from random import randint
import time
class test():
@staticmethod
def print1():
time.sleep(5)
print("this message is from print1 ")
@staticmethod
def print2():
print("this message is from print2 ")
def display(name, count):
if count > 1:
print "Now, %s has %d apples.\n" % (name, count),
elif count == 1:
print "Now, %s has an apple.\n" % name,
else:
print "Now, %s has not any apples.\n" % name,
def eat_apple(name, count):
display(name, count)
while count > 0:
print "%s eats an apple.\n" % name,
count -= 1
display(name, count)
sleep(randint(1, 3))
class MyThread(Thread):
"""docstring for MyThread"""
def __init__(self, name, count):
super(MyThread, self).__init__()
self.name = name
self.count = count
def run(self):
if self.count == 1:
# pass
test.print1(1)
else:
# pass
test.print2(2)
# huey = MyThread("Huey", 1)
# sugar = MyThread("Sugar", 5)
# huey.start()
# sugar.start()
threads = []
threads.append(threading.Thread(target=test.print1, args=()))
threads.append(threading.Thread(target=test.print2, args=()))
for t in threads:
t.start()