-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path多线程.py
69 lines (62 loc) · 1.67 KB
/
多线程.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
__author__ = '財'
import threading
from threading import current_thread
# def myThread(arg1, arg2):
# print(current_thread().getName(),'start')
# print('%s %s'%(arg1, arg2))
#
# for i in range(1,10000,1):
# # t1 = myThread(i, i+1)
# t1 = threading.Thread(target=myThread,args=(i, i+1))
# t1.start()
# class Mythreads(threading.Thread):
# def run(self):
# print(current_thread().getName(),'start')
# print('run')
# print(current_thread().getName(),'stop')
#
# t1 = Mythreads()
# t1.start()
# # t1.join()
#
# print(current_thread().getName(),'end')
from threading import Thread,current_thread
import time
import random
from queue import Queue
queue = Queue(5)
class Producer(Thread):
def run(self):
name = current_thread().getName()
nums = range(100)
global queue
while True:
num = random.choice(nums)
queue.put(num)
print('生产者 %s 生产了数据 %s' %(name,num))
t = random.randint(1,3)
time.sleep(t)
print('生产者 %s 睡眠了 %s 秒' %(name,t))
class Custmer(Thread):
def run(self):
name = current_thread().getName()
global queue
while True :
num = queue.get()
queue.task_done()
print('消费者 %s 消耗了数据 %s' %(name,num))
t = random.randint(1,5)
time.sleep(t)
print('消费者 %s 睡眠了 %s' %(name, t))
p1 = Producer(name= 'p1')
p1.start()
p2 = Producer(name= 'p2')
p2.start()
p3 = Producer(name= 'p3')
p3.start()
p4 = Producer(name= 'p4')
p4.start()
c1 = Custmer(name= 'c1')
c1.start()
c2 = Custmer(name= 'c2')
c2.start()