-
Notifications
You must be signed in to change notification settings - Fork 1
/
pythonThreading.py
74 lines (64 loc) · 1.52 KB
/
pythonThreading.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# import threading
# import time
#
# exitFlag = 0
#
# class myThread (threading.Thread):
# def __init__(self, threadID, name, counter):
# threading.Thread.__init__(self)
# self.ID = threadID
# self.name = name
# self.counter = counter
# def run(self):
# print ("Starting " + self.name)
# print_time(self.name, self.counter, 5)
# print ("Exiting " + self.name)
#
# def print_time(threadName, delay, counter):
# while counter:
# if exitFlag:
# pass
# # threadName.exit()
# time.sleep(delay)
# print ("%s: %s" % (threadName, time.ctime(time.time())))
# counter -= 1
#
# # Create new threads
# thread1 = myThread(1, "Thread-1", 1)
# thread2 = myThread(2, "Thread-2", 2)
#
# # Start new Threads
# thread1.start()
# thread2.start()
# thread1.join()
# thread2.join()
# print ("Exiting Main Thread")
import threading
from time import sleep
class Demo(threading.Thread):
def __init__(self, name, delay):
threading.Thread.__init__(self)
self.delay = delay
def run(self):
# self.print_data(self.threadName)
i = 0
while True:
print(i, " ")
sleep(self.delay)
i += 1
if i == 7:
break
def print_data(self, name):
i = 0
while True:
print(i, " ", name)
sleep(2)
i += 1
if i == 7:
break
t1 = Demo("first", 1)
t2 = Demo("second", 2)
t1.start()
t2.start()
t1.join()
t2.join()