-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread_ex.py
73 lines (58 loc) · 1.54 KB
/
Thread_ex.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
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 27 15:06:54 2023
@author: alexi
"""
from threading import Thread, Event, Lock
import time, datetime
class A:
def __init__(self,id_ = 0):
self.id_ = id_
self.event = Event()
self.event.set()
self.thread = Thread(target=self.foo, daemon = True)
def foo(self):
while True:
self.event.wait()
print(self.id_)
time.sleep(0.1)
def goo(self):
self.event.clear()
for i in range(10):
print('goo')
time.sleep(0.5)
self.event.set()
def lock(self):
self.event.clear()
def realease(self):
self.event.set()
def func(self):
self.thread.start()
self.goo()
class B:
def __init__(self, id_ = 0):
self.count = 0
self.lock = Lock()
self.thread = Thread(target=self.foo, daemon = True)
self.id_ = id_
def foo(self,val = 0):
for i in range(30):
with self.lock:
print(val)
time.sleep(0.1)
def goo(self):
with self.lock:
for i in range(10):
print('goo')
time.sleep(0.5)
self.foo(val= 20)
def lock(self):
self.event.clear()
def realease(self):
self.event.set()
def func(self):
self.thread.start()
self.goo()
if __name__ == '__main__':
b1 = B(1)
b1.func()