上一篇文章最后只是简单介绍了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. java web工程web.xml介绍

    转载自:http://blog.csdn.net/believejava/article/details/43229361 Web.xml详解: 1.web.xml加载过程(步骤) 首先简单讲一下,w ...

  2. VPS虚拟化架构OpenVZ、KVM、Xen、Hyper-V的区别

    1.OpenVZ OpenVZ(简称OVZ)采用SWsoft的Virutozzo虚拟化服务器软件产品的内核,是基于Linux平台的操作系统级服务器虚拟化架构.这个架构直接调用宿主机(俗称:母机)中的内 ...

  3. zoj 1037 最短路

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=37 找规律,水题 #include<iostream> #inclu ...

  4. 图解 CMS 垃圾回收机制,你值得拥有(转 强烈推荐)

          首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 Java小组 工具资源 - 导航条 - 首页 所有文章 资讯 Web 架构 基础技术 书籍 教程 Java小组 工具资源     ...

  5. NodeList、HTMLCollection和NamedNodeMap

    上篇文章以arguments为例讲到了类数组对象,这篇我们讨论更多的类数组对象NodeList.HTMLCollection和NamedNodeMap.既然是类数组对象,这3种对象也都能应用上篇文章中 ...

  6. js array copy method

    //浅拷贝: let arr=[1,2,3,4] let arr2=arr arr[3]=0 console.log(arr,arr2) //output: (4) [1, 2, 3, 0] (4) ...

  7. position布局影响点击事件以及冒泡获取事件目标

    在编写功能时总是会出现很多意想不到的问题,现在就讲讲我遇到的两个问题,通过举一个相似的例子来解说. <1> 元素互相独立,不存在包含于被包含 选择城市的按钮,为它绑定点击事件,点击后就弹出 ...

  8. Git 拉取Gitee仓库报错:“fatal: unable to access ''": Failed to connect to 127.0.0.1 port 1080: Connection refused”

    1.报错信息: 2.本地查看是否Git使用了代理 git config --global http.proxy 3.取消代理 git config --global --unset http.prox ...

  9. ArcGIS 地类净面积计算工具

    地类净面积计算工具可以自己定义图层.字段.地类代码计算任意图层的椭球面积.线状地物扣除.零星扣除和其他扣除,计算地类净面积计算:可以用于二调数据图斑地类.规划地块和基本农田等等需要计算净面积的都可以. ...

  10. Heap memory compared to stack memory