上一篇文章最后只是简单介绍了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()方法的区别(三)的更多相关文章

  1. Python之路(第四十二篇)线程相关的其他方法、join()、Thread类的start()和run()方法的区别、守护线程

    一.线程相关的其他方法 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getName(): 返回线程名. # setName(): 设置线程名. ​ threadin ...

  2. 认识多线程中start和run方法的区别?

    一.认识多线程中的 start() 和 run() 1.start(): 先来看看Java API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并 ...

  3. Java -- Thread中start和run方法的区别

    一.认识Thread的 start() 和 run() 1.start(): 我们先来看看API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并发 ...

  4. 线程的状态有哪些,线程中的start与run方法的区别

    线程在一定条件下,状态会发生变化.线程一共有以下几种状态: 1.新建状态(New):新创建了一个线程对象. 2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法. ...

  5. 调用start()方法和直接调用run()方法的区别

    调用start()方法和直接调用run()方法的区别 新建一个线程,只需要使用new关键字创建一个线程对象,并且调用start()方法即可. Thread thread = new Thread(); ...

  6. 多线程 start 和 run 方法到底有什么区别?

    昨天栈长介绍了<Java多线程可以分组,还能这样玩!>线程分组的妙用.今天,栈长会详细介绍 Java 中的多线程 start() 和 run() 两个方法,Java 老司机请跳过,新手或者 ...

  7. 【Java_多线程并发编程】基础篇—Thread类中start()和run()方法的区别

    1. start() 和 run()的区别说明 start()方法: 它会启动一个新线程,并将其添加到线程池中,待其获得CPU资源时会执行run()方法,start()不能被重复调用. run()方法 ...

  8. 多线程应用之调用start()方法和run()方法的区别

    今天在做项目的时候,遇到一个问题,两个一模一样的demo,运行出来的效果却一点也不一样,找了半天,就是有一行代码不同,一个是thread.start();一个是thread.run();和我预计的一样 ...

  9. Java中start和run方法的区别

    一.问题引入         说到这两个方法就不得不说多线程,说到多线程就不得不提实现多线程的两种方式继承Thread类和实现Runable接口,下面先看这两种方式的区别. 二. Java中实现多线程 ...

随机推荐

  1. 标准Trie字典树学习一:原理解析

    特别声明: 博文主要是学习过程中的知识整理,以便之后的查阅回顾.部分内容来源于网络(如有摘录未标注请指出).内容如有差错,也欢迎指正! 系列文章: 1. 字典树Trie学习一:原理解析 2.字典树Tr ...

  2. 六、yarn运行模式

    简介 spark的yarn运行模式根据Driver在集群中的位置分成两种: 1)yarn-client 客户端模式 2)yarn-cluster 集群模式 yarn模式和standalone模式不同, ...

  3. 用户登录注册(安全)(常规、FB、google、paypal) 实战

    /* 用户登录界面 */elseif ($action == 'login'){    if($_SESSION['user_id'])    {        ecs_header("Lo ...

  4. C#语言总结1

    C#C#定义: C#是一门面向对象.面向组件的一门语言,是.NET的一部分. 程序结构: 命名空间 类{ 属性 方法 main函数入口 } 数据类型: C#的数据类型分为:值类型(Value type ...

  5. SQL Server Metadata

    http://www.devart.com/dotconnect/sqlserver/docs/MetaData.htmlhttps://msdn.microsoft.com/en-us/librar ...

  6. JavaScript周报#184

    This week’s JavaScript news Read this issue on the Web | Issue Archive JavaScript Weekly Issue 184Ju ...

  7. Swift Development – List of Resources You Must Bookmark

    Ever since the introduction of iOS, there is iOS development fever across the globe. Many iOS develo ...

  8. HTTP协议教程

    文章内容: 1.HTTP协议概述 2.URL知识概述 3.HTTP消息结构详解 1.HTTP协议概述 定义: 超文本传送协议 (HTTP-Hypertext transfer protocol) 是分 ...

  9. 微信小程序-03-小程序开发框架

    微信小程序-03-小程序开发框架 官方文档: https://developers.weixin.qq.com/miniprogram/dev/framework/MINA.html 小程序开发框架 ...

  10. vim常用快捷汇总

    移动光标的方法 h 或 向左箭头键(←) 光标向左移动一个字符 j 或 向下箭头键(↓) 光标向下移动一个字符 k 或 向上箭头键(↑) 光标向上移动一个字符 l 或 向右箭头键(→) 光标向右移动一 ...