Python多线程简单例子

来源:互联网 发布:叉叉助手获取数据失败 编辑:程序博客网 时间:2024/05/22 17:50

直接附上代码:

#!/usr/bin/python# coding:utf-8import threadingimport timeclass Test(threading.Thread):    def __init__(self):        threading.Thread.__init__(self)        # self._run_num = num    def run(self):        global count, mutex        threadname = threading.currentThread().getName()        # for x in xrange(0, int(self._run_num)):        mutex.acquire()        count += 1        mutex.release()        print '线程信息名字:', threadname        print '在这个线程中的编号: ', x        print '累计和: ', count        # time.sleep(1)global count, mutexthreads = []num = 4count = 0mutex = threading.Lock()for x in xrange(0, num):    threads.append(Test())for t in threads:    t.start()for t in threads:    t.join()
0 0
原创粉丝点击