代码改变世界

多线程

2018-07-05 18:02  改改~_~  阅读(248)  评论(0编辑  收藏  举报
 1 import _thread
 2 import time
 3 
 4 def print_time(threadName,delay):
 5     count = 0
 6     while count < 5:
 7         time.sleep(delay)
 8         count += 1
 9         print('{0}: {1}'.format(threadName,time.ctime(time.time())))
10 
11 try:
12     _thread.start_new_thread(print_time,('Thread-1',2,))
13     _thread.start_new_thread(print_time,('Thread-2',4,))
14 except:
15     print('Error')
16 while 1:
17     pass

 

 

 1 # 3-线程同步
 2 import threading
 3 import time
 4 class myThread(threading.Thread):
 5     def __init__(self,threadID,name,counter):
 6         threading.Thread.__init__(self)
 7         self.threadID = threadID
 8         self.name = name
 9         self.counter = counter
10     def run(self):
11         print('开启线程: ' + self.name)
12         threadLock.acquire()
13         print_time(self.name,self.counter,3)
14         threadLock.release()
15 
16 def print_time(threadName,delay,counter):
17     while counter:
18         time.sleep(delay)
19         print('{} :{}'.format(threadName,time.ctime(time.time())))
20         counter -=1
21 
22 threadLock = threading.Lock()
23 threads  = []
24 
25 thread1 = myThread(1,'Thread-1',1)
26 thread2 = myThread(2,'Thread-2',2)
27 
28 thread1.start()
29 thread2.start()
30 
31 threads.append(thread1)
32 threads.append(thread2)
33 
34 for t in threads:
35     t.join()
36 print('退出主线程')

 

 1 # 线程优先级队列
 2 import queue
 3 import threading
 4 import time
 5 
 6 exitFlag = 0
 7 
 8 class myThread (threading.Thread):
 9     def __init__(self,threadID,name,q):
10         threading.Thread.__init__(self)
11         self.thredID = threadID
12         self.name = name
13         self.q = q
14     def run(self):
15         print('开启线程:' + self.name)
16         process_data(self.name,self.q)
17         print('退出线程: ' + self.name)
18 def process_data(threadName,q):
19     while not exitFlag:
20         queueLock.acquire()
21         if not workQueue.empty():
22             data = q.get()
23             queueLock.release()
24             print('{}: {}'.format(threadName,data))
25         else:
26             queueLock.release()
27         time.sleep(1)
28 
29 threadList = ['Thread-1','Thread-2','Thread-3']
30 nameList = ['One','Two','Three','Four','Five']
31 queueLock = threading.Lock()
32 workQueue = queue.Queue(10)
33 threads = []
34 threadID = 1
35 
36 for tName in threadList:
37     thread = myThread(threadID,tName,workQueue)
38     thread.start()
39     threads.append(thread)
40     threadID += 1
41 
42 queueLock.acquire()
43 for word in nameList:
44     workQueue.put(word)
45 queueLock.release()
46 
47 while not workQueue.empty():
48     pass
49 
50 exitFlag = 1
51 
52 for t in threads:
53     t.join()
54 print('退出主鼎城')

 

 

 1 # 使用threading模块创建线程
 2 import threading
 3 import time
 4 
 5 exitFlag = 0
 6 class myThread(threading.Thread):
 7     def __init__(self,threadID,name,counter):
 8         threading.Thread.__init__(self)
 9         self.threadID = threadID
10         self.name = name
11         self.counter = counter
12     def run(self):
13         print('开始线程: '+ self.name)
14         print_time(self.name,self.counter,5)
15         print('tichuxiancheng') + self.name
16 
17 def print_time(threadName,delay,counter):
18     while counter:
19         if exitFlag:
20              threadName.exit()
21         time.sleep(delay)
22         print('{}:{}'.format(threadName,time.ctime(time.time())))
23 
24 
25 thread1 = myThread(1,'Thread-1',1)
26 thread2 = myThread(2,'Thread-2',2)
27 
28 thread1.start()
29 thread2.start()
30 thread1.join()
31 thread2.join()
32 print('退出主线程')