python 多线程

# -*- coding: utf-8 -*-
#!/usr/bin/python
import urllib2
import urllib
import base64
import sys
import time
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

import threading

class Test(threading.Thread):
    def __init__(self, num):
        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 = count + 1
            mutex.release()
            print threadname, x, count,time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())

global count, mutex

threads= []

num = 300

count = 1
    # 创建锁

mutex = threading.Lock()
    # 创建线程对象
for x in xrange(0, num):

  threads.append(Test(4))
     # 启动线

for t in threads:
      t.start()
    # 等待子线程结束
for t in threads:
      t.join()

你可能感兴趣的:(多线程,python)