并发编程——线程——线程的创建

一、threading模块介绍

之前讲进程的时候介绍过multiprocess包,threading包的接口跟mulitprocess包的接口是类似的,二者在使用层面,有很大的相似性。

二、Thread

threading包中的Thread模块可以创建线程。

先看一下源码:

class Thread:
    """A class that represents a thread of control.
	   表示控制线程的类。
    This class can be safely subclassed in a limited fashion. 
    这个类可以安全地以有限的方式进行子类化。
    There are two ways to specify the activity: by passing a callable object to the constructor, or by overriding the run() method in a subclass.
	有两种方法可以指定活动:将可调用对象传递给构造函数,或重写子类中的run()方法。
    """

类的头注释表明了两种创建线程的方式:

1.通过Thread类直接实例化对象,给对象传递构造函数

2.继承Thread类并重写子类的run方法

    

猜你喜欢

转载自blog.csdn.net/weixin_43336281/article/details/104207812