python 多线程例子

来源:互联网 发布:数组去重js算法实现 编辑:程序博客网 时间:2024/05/22 15:59
#!/usr/bin/env python# encoding: utf-8import sysimport threadimport timeimport threadingreload(sys).setdefaultencoding('UTF-8')import osimport logging"""@version: @author: @license: @contact: @site: @software: PyCharm@file: thread_test.py@time: 2016/11/24 10:10"""def func():    passclass Main():    def __init__(self):        passdef print_time(threadName, delay):    count = 0    while count < 5 :        time.sleep(delay)        count += 1        print "%s: %s" % (threadName, time.ctime(time.time()))# 创建两个线程# try:#     thread.start_new_thread(print_time, ("Thread-1", 2))#     thread.start_new_thread(print_time, ("Thread-2", 4))## except:#     print 'error'## while 1:#     passclass myThread(threading.Thread):    def __init__(self, threadID, name, counter):        threading.Thread.__init__(self)        self.threadID = threadID        self.name = name        self.counter = counter    def run(self):        print 'start'        print_time(self.name, self.counter, 5)        print 'end'def print_time(threadName, delay, counter):    while counter:        print "%s: %s" % (threadName, time.ctime(time.time()))        time.sleep(delay)        counter -= 1# 创建新线程thread1 = myThread(1, "Thread-1", 10)thread2 = myThread(2, "Thread-2", 2)# 开启线程thread1.start()thread2.start()if __name__ == '__main__':    pass

0 0
原创粉丝点击