#!/usr/bin/evn python

# -*- coding:utf-8 -*-

import threading

import multiprocessing

import Queue

import time

 

class myThread(threading.Thread):

def run(self):

print("Thread-"+self.name+" Start.........................\n")

threadActionfun(self.name,self.q)

print("Thread-"+self.name+" End..........................\n")

 

threadlock=threading.Lock();

 

messages=Queue.Queue();

 

threadNames=['thread-One','thread-Two','thread-Three']

actionlist=['one','two','three','four','fifair']

isEnd=0

"""

把需要处理的数据添加到相应的队列当中

"""

threadlock.acquire()

for actionstr in actionlist:

messages.put(actionstr);

threadlock.release();

 

def threadActionfun(name,q):

while not isEnd:

"""

使用线程锁把数据逐个拿出来

"""

q.acquire()

if not messages.empty():

messagestr = messages.get();

print("simon--" + name + " action :" + messagestr+"\n");

q.release();

time.sleep(1);

 

threads=[];

# 初始化线程数据

for threadName in threadNames:

threadobj=myThread();

threadobj.q=threadlock;

threadobj.name=threadName;

threadobj.start();

threads.append(threadobj)

 

while not messages.empty():

pass

isEnd=1;

print("已经使用IsEnd了")

 

# 等待子线程完成

for thread in threads:

thread.join();

 

print("本机cpu数量为:"+str(multiprocessing.cpu_count()))

 

 

 

 

 

 

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐