Python 多线程 start()和run()方法的区别(三)
上一篇文章最后只是简单介绍了start()方法和run()方法,这篇文章再详细地看下start()和run()的区别。
在实例调用的函数中加入打印当前线程的名字,分别用start()方法和run()方法启动线程检查有什么区别:
start()方法:
import threading
import time def worker():
count = 1
while True:
if count >= 6:
break
time.sleep(1)
count += 1
print("thread name = {}".format(threading.current_thread().name))#当前线程名 t = threading.Thread(target=worker,name="MyThread")
t.start() print("===end===") 运行结果:
===end===
thread name = MyThread #
thread name = MyThread
thread name = MyThread
thread name = MyThread
thread name = MyThread
从上面例子中打印的线程名字来看,使用start()方法启动的线程名是我们定义线程对象时设置的name="MyThread"的值,如果没有设置name参数值,则会打印系统分配的Thread-1,Thread-2...这样的名称。
run()方法:
import threading
import time def worker():
count = 1
while True:
if count >= 6:
break
time.sleep(1)
count += 1
print("thread name = {}".format(threading.current_thread().name)) t = threading.Thread(target=worker,name="MyThread")
t.run() print("===end===") 运行结果:
thread name = MainThread #
thread name = MainThread
thread name = MainThread
thread name = MainThread
thread name = MainThread
===end===
上面例子中,使用的是用run()方法启动线程,它打印的线程名是MainThread,也就是主线程。
再看下多线程时的例子:
start():
import threading
import time def worker():
count = 1
while True:
if count >= 6:
break
time.sleep(1)
count += 1
print("thread name = {}, thread id = {}".format(threading.current_thread().name,threading.current_thread().ident)) t1 = threading.Thread(target=worker,name="t1")
t2 = threading.Thread(target=worker,name='t2') t1.start()
t2.start() print("===end===") 运行结果:
===end===
thread name = t1, thread id = 6032
thread name = t2, thread id = 880
thread name = t1, thread id = 6032
thread name = t2, thread id = 880
thread name = t2, thread id = 880
thread name = t1, thread id = 6032
thread name = t1, thread id = 6032
thread name = t2, thread id = 880
thread name = t2, thread id = 880
thread name = t1, thread id = 6032
上面例子中,start()方法启动了两个新的子线程并交替运行,每个子进程ID也不同。
run():
import threading
import time def worker():
count = 1
while True:
if count >= 6:
break
time.sleep(1)
count += 1
print("thread name = {}, thread id = {}".format(threading.current_thread().name,threading.current_thread().ident)) t1 = threading.Thread(target=worker,name="t1")
t2 = threading.Thread(target=worker,name='t2') t1.run()
t2.run() print("===end===") 运行结果:
thread name = MainThread, thread id = 2000
thread name = MainThread, thread id = 2000
thread name = MainThread, thread id = 2000
thread name = MainThread, thread id = 2000
thread name = MainThread, thread id = 2000
thread name = MainThread, thread id = 2000
thread name = MainThread, thread id = 2000
thread name = MainThread, thread id = 2000
thread name = MainThread, thread id = 2000
thread name = MainThread, thread id = 2000
===end===
上面例子中,两个子线程都用run()方法启动,但却是先运行t1.run(),运行完之后才按顺序运行t2.run(),两个线程都工作在主线程,没有启动新线程,因此,run()方法仅仅是普通函数调用。
一个进程中至少有一个线程,并作为程序的入口,这个线程就是主线程。
一个进程至少有一个主线程,其它线程称为工作线程。
总结:
好了,从上面四个小例子,我们可以总结出:
- start() 方法是启动一个子线程,线程名就是我们定义的name
- run() 方法并不启动一个新线程,就是在主线程中调用了一个普通函数而已。
因此,如果你想启动多线程,就必须使用start()方法。
Python 多线程 start()和run()方法的区别(三)的更多相关文章
- Python之路(第四十二篇)线程相关的其他方法、join()、Thread类的start()和run()方法的区别、守护线程
一.线程相关的其他方法 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getName(): 返回线程名. # setName(): 设置线程名. threadin ...
- 认识多线程中start和run方法的区别?
一.认识多线程中的 start() 和 run() 1.start(): 先来看看Java API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并 ...
- Java -- Thread中start和run方法的区别
一.认识Thread的 start() 和 run() 1.start(): 我们先来看看API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并发 ...
- 线程的状态有哪些,线程中的start与run方法的区别
线程在一定条件下,状态会发生变化.线程一共有以下几种状态: 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法. ...
- 调用start()方法和直接调用run()方法的区别
调用start()方法和直接调用run()方法的区别 新建一个线程,只需要使用new关键字创建一个线程对象,并且调用start()方法即可. Thread thread = new Thread(); ...
- 多线程 start 和 run 方法到底有什么区别?
昨天栈长介绍了<Java多线程可以分组,还能这样玩!>线程分组的妙用.今天,栈长会详细介绍 Java 中的多线程 start() 和 run() 两个方法,Java 老司机请跳过,新手或者 ...
- 【Java_多线程并发编程】基础篇—Thread类中start()和run()方法的区别
1. start() 和 run()的区别说明 start()方法: 它会启动一个新线程,并将其添加到线程池中,待其获得CPU资源时会执行run()方法,start()不能被重复调用. run()方法 ...
- 多线程应用之调用start()方法和run()方法的区别
今天在做项目的时候,遇到一个问题,两个一模一样的demo,运行出来的效果却一点也不一样,找了半天,就是有一行代码不同,一个是thread.start();一个是thread.run();和我预计的一样 ...
- Java中start和run方法的区别
一.问题引入 说到这两个方法就不得不说多线程,说到多线程就不得不提实现多线程的两种方式继承Thread类和实现Runable接口,下面先看这两种方式的区别. 二. Java中实现多线程 ...
随机推荐
- Spring MVC 实现Excel的导入导出功能(1:Excel的导入)
简介 这篇文章主要记录自己学习上传和导出Excel时的一些心得,企业办公系统的开发中,经常会收到这样的需求:批量录入数据.数据报表使用 Excel 打开,或者职能部门同事要打印 Excel 文件,而他 ...
- 从Linux系统安装到Web应用启动教程
概述 本文讲述web应用服务器安装配置教程,其中包括:Linux系统安装,Mysql数据库安装配置,Redis安装配置,Tomcat安装配置,MongoDB安装配置,Linux JDK安装使用,Ngi ...
- HDU 1875(最小生成树)
因为是全连接图,所以也可以用最小生成树 这道题给边加了一个限制条件,(10<=x<=1000),所以可能不能全连通,需要判断 #include <cstdio> #includ ...
- java设计模式-----7、装饰模式
首先,什么是装饰者模式呢??? 装饰( Decorator )模式又叫做包装模式.通过一种对客户端透明的方式来扩展对象的功能,是继承关系的一个替换方案.他是23种设计模式之一,英文叫Decorator ...
- Oracle查看表空间容量
select /*+ no_merge */ al.tablespace_name, round(al.currsizemb) currsizemb, round(al.maxsizemb) maxs ...
- html-路径的介绍
一.绝对路径 绝对路径是指文件在硬盘上真正存在的路径 - D:\Java\JavaWeb\day01\a.jpg - http://www.baidu.com/b.jpg 二.相对路径 一个文件相对于 ...
- bootstrap学习笔记(网页开发小知识)
这是我在学习Boostrap网页开发时遇到的主要知识点: 1.导航条navbar 添加.navbar-fixed-top类可以让导航条固定在顶部,固定的导航条会遮住页面上的其他内容,除非给<bo ...
- canvas toDataURL() 方法如何生成部分画布内容的图片
HTMLCanvasElement.toDataURL() 方法返回一个包含图片展示的 data URI .可以使用 type参数其类型,默认为 PNG 格式.图片的分辨率为96dpi. 如果画布的高 ...
- NGINX本地服务器解析域名
1.找到hosts文件 ,添加需要解析的域名 2.在cmd命令窗口中检测解析是否生效 3 找到本地服务器的域名配置文件:添加绑定的域名,更改访问的目录 4.添加pathinfo.隐藏index.php ...
- 浏览器组成、线程及event loop
浏览器组成 User interface: a. Every part of the browser display, except the window. b. The address bar, b ...