-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusingLocks.py
More file actions
56 lines (37 loc) · 1.08 KB
/
Copy pathusingLocks.py
File metadata and controls
56 lines (37 loc) · 1.08 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
#!usr/bin/python
import threading
import time
#exitFlag = 0
class mythread(threading.Thread):
def __init__(self, threadName, delay):
self.threadName = threadName
self.delay = delay
threading.Thread.__init__(self)
def run(self):
print "starting:" + self.threadName
ThreadLock.acquire()
print_time(self.threadName, self.delay, 5)
ThreadLock.release()
#print "exiting " + self.name
def print_time(threadName, delay, counter):
while counter:
#if exitFlag:
# thread.exit()
time.sleep(delay)
print "%s %s" %(threadName, time.ctime(time.time()))
counter-=1
ThreadLock = threading.Lock()
threads=[]
t_1 = mythread("thread-1", 2)
t_2 = mythread("thread-2", 2)
threads.append(t_1)
threads.append(t_2)
t_1.start()
t_2.start()
#while t_1.isAlive():
# if not t_2.isAlive():
# exitFlag = 1
# pass
for t in threads:
t.join()
print "exiting the main thread"