demo1

#!/usr/bin/python
# -*- coding: UTF-8 -*-import time
from threading import Thread
# 自定义线程函数。
def main(name="Python"):for i in range(2):print("hello", name)time.sleep(1)# 创建线程01,不指定参数
thread_01 = Thread(target=main)
# 启动线程01
thread_01.start()# 创建线程02,指定参数,注意逗号
thread_02 = Thread(target=main, args=("MING",))
# 启动线程02
thread_02.start()

demo2

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
from threading import Thread
class MyThread(Thread):def __init__(self, name="Python"):# 注意,super().__init__() 一定要写# 而且要写在最前面,否则会报错。super().__init__()self.name=namedef run(self):for i in range(2):print("hello", self.name)time.sleep(1)if __name__ == '__main__':# 创建线程01,不指定参数thread_01 = MyThread()# 创建线程02,指定参数thread_02 = MyThread("MING")thread_01.start()thread_02.start()

demo3

#!/usr/bin/python
# -*- coding: UTF-8 -*-import threading
import timeexitFlag = 0class myThread (threading.Thread):   #继承父类threading.Threaddef __init__(self, threadID, name, counter):threading.Thread.__init__(self)self.threadID = threadIDself.name = nameself.counter = counterdef run(self):                   #把要执行的代码写到run函数里面 线程在创建后会直接运行run函数 print "Starting " + self.nameprint_time(self.name, self.counter, 5)print "Exiting " + self.namedef print_time(threadName, delay, counter):while counter:if exitFlag:(threading.Thread).exit()time.sleep(delay)print "%s: %s" % (threadName, time.ctime(time.time()))counter -= 1# 创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)# 开启线程
thread1.start()
thread2.start()print "Exiting Main Thread"

参考:
https://www.cnblogs.com/wongbingming/p/9028851.html
https://www.runoob.com/python/python-multithreading.html

转载于:https://www.cnblogs.com/sea-stream/p/11178904.html

python 多线程Thread相关推荐

  1. Python多线程thread与threading实现

    python是支持多线程的,并且是native的线程.主要是通过thread和threading这两个模块来实现的. python的thread模块是比较底层的模块,python的threading模 ...

  2. python queue threading_Python 多线程 -thread threading Queue- 简单学习

    Python 多线程 -thread threading Queue- 简单学习 在实际工作过程中,会出现需要并发的做一些事情,例如一台机器测到几千台机器的网络连通性,如果你单线程一台一台测的话,会花 ...

  3. python 多线程 线程如何获得返回值 (重写threading.Thread类)

    重写threading.Thread类: # -*- coding: utf-8 -*- """ @File : dontla_ThreadClass.py @Time ...

  4. Python threading Thread多线程的使用方法

    Python threading Thread多线程的使用方法 参考资料:<Python 多线程>http://www.runoob.com/python/python-multithre ...

  5. python 多线程并发怎么还是顺序执行_python thread 并发且顺序运行示例

    python怎么实现用多线程顺序执行 python 的GIL规定每个时刻只能有一个线程访问python虚拟机,所以你要用python的多线程来做计算是很不合算的,但是对于IO密集型的应用,例如网络交互 ...

  6. python的多线程threading_Python中多线程thread与threading的实现方法,pythonthreading

    Python中多线程thread与threading的实现方法,pythonthreading 学过Python的人应该都知道,Python是支持多线程的,并且是native的线程.本文主要是通过th ...

  7. Python中多线程thread与threading的实现方法

    Python中多线程thread与threading的实现方法 这篇文章主要介绍了Python中多线程thread与threading的实现方法,很重要的应用,需要的朋友可以参考下 学过Python的 ...

  8. Python多进程(process)和多线程(thread)的区别

    目录 一.线程与进程 1.基本概念 2.区别 二.多进程与多线程 1.多进程 (1)Python的多进程编程与multiprocess模块 (2)利用multiprocess模块的Pool类创建多进程 ...

  9. Python多线程编程---(1)threading 模块 Thread 类

    全文参考:Python多线程编程(一):threading 模块 Thread 类的用法详解_frank 的专栏-CSDN博客 最近看了下团队自动化测试用例的代码,里面有涉及并行处理的逻辑,主要是基于 ...

最新文章

  1. linux 观察应用使用内存的情况,Linux学习笔记:free和top命令查看系统内存使用情况...
  2. scanf_s()函数 (是Microsoft公司VS开发工具提供的一个功能相同的安全标准输入函数)
  3. JDK源码解析-Collection.iterator方法
  4. intellij中springboot热部署
  5. 抓包(Charles工具入门)
  6. java定义一个方法,返回一个整数数组的和
  7. ESXi 中重新启动管理代理
  8. PHP是迄今为止最好的web平台
  9. 【极客】神器——常用的一些小工具
  10. 500个运营工具大全,速度收藏!!!
  11. 基于用户喜好以及电影类型的电影推荐算法
  12. 2020计算机毕设选题推荐可视化方向,前端方向本科应届生有什么毕设选题推荐?...
  13. MacBook M1 Windows for ARM虚拟机体验
  14. 记一次跟二房东公司(非中介个人房源无中介费)租房的经历
  15. 电商-支付相关表的表设计
  16. 走遍美国 下载(78集)
  17. python学习(七)----time模块相关函数
  18. 二级页表分页存储需要目录页表数形象解释
  19. Android接入支付宝实现支付功能
  20. 【英语六级笔记】翻译部分

热门文章

  1. qcustomplot圆_QCustomPlot使用心得五:坐标轴常用属性设置
  2. mysql 1067 win7,大师练习win7系统无法启动MySQL服务错误1067的设置教程
  3. hue集成mysql报错_hue集成hive访问报database is locked
  4. python setupools
  5. selection.text
  6. 数据结构之基于Java的顺序栈实现
  7. windows下安装HTK3.4
  8. 支付业务与技术架构学习总结(6)——对账系统的设计
  9. Linux学习总结(51)——25个Linux服务器安全小贴士
  10. Java基础学习总结(106)——高级JAVA工程师必需技能