多线程-threading.Thread
来源:互联网 发布:sql sever 视图创建 编辑:程序博客网 时间:2024/05/21 15:40
线程中的全局变量是不安全的,局部变量是安全的,各自线程保有。
查看线程数量:
length = len(threading.enumerate())
方式一:
import threading from time import sleep def test(sleepTime): num=1 sleep(sleepTime) num+=1 print('---(%s)--num=%d'%(threading.current_thread(), num)) t1 = threading.Thread(target = test,args=(5,)) t2 = threading.Thread(target = test,args=(1,)) t1.start() t2.start()
方式二:
#coding=utf-8 import threading import time class MyThread(threading.Thread): # 重写 构造方法 def __init__(self,num,sleepTime): threading.Thread.__init__(self) #重写父类的初始化方法 self.num = num self.sleepTime = sleepTime def run(self): self.num += 1 time.sleep(self.sleepTime) print('线程(%s),num=%d'%(self.name, self.num)) if __name__ == '__main__': t1 = MyThread(100,5) t1.start() t2 = MyThread(200,1) t2.start()
阅读全文
0 0
- 多线程-threading.Thread
- Python多线程:thread、threading、queue
- python多线程模块thread,threading,Queue
- Python多线程thread与threading实现
- python多线程thread/threading的使用
- Python多线程thread与threading实现
- Python多线程thread与threading实现
- .NET多线程编程 - System.Threading.Thread类
- python threading模块/thread多线程(1)
- Python多线程编程之thread和threading
- threading.Thread
- .NET多线程编程(2):System.Threading.Thread类
- .NET多线程编程(2):System.Threading.Thread类
- .NET多线程编程(2):System.Threading.Thread类
- .NET多线程编程(2):System.Threading.Thread类
- .NET多线程编程(2):System.Threading.Thread类
- .NET多线程编程(2):System.Threading.Thread类
- .NET多线程编程(2):System.Threading.Thread类
- 51nod 最大子段和(动态规划DP)
- 由数电课设到编程---如何改bug
- 敏捷和CMMI对比——译文
- Treeset(自然顺序的实现)
- Vue路由和嵌套路由介绍。。。
- 多线程-threading.Thread
- HDU4456-Crowd
- ActiveMQ实战(一)--ActiveMQ简介
- 《利用Python进行数据分析》学习笔记ch04(5)
- windows 2008 服务器 安全设置
- 三体【经典语句】
- 博客搬家
- 链表之通讯录
- 元素出栈、入栈顺序的合法性