- 14
- 0
- 约小于1千字
- 约 13页
- 2019-12-23 发布于湖北
- 举报
知识点: 函数运行在多线程的两种实现
;;;;调用 thread 模块中的 start_new_thread() 函数来创建线程,以线程函数的形式告诉线程该做什么。;# -*- coding: utf-8 -*-import threaddef f(name):? #定义线程函数? print “this is ” + nameif __name__ == ‘__main__’:? thread.start_new_thread(f, (“thread1”,))? #用start_new_thread()调用线程函数和参数? while 1:??? pass;;;调用 threading 模块,创建 threading.Thread 的子类来得到自定义线程类。
# -*- coding: utf-8 -*-import threadingclass Th(threading.Thread):def __init__(self, name):??? threading.Thread.__init__(self)??? self.t_name = name??? #调用父类构造函数;def run(self): #重写run()函数,线程默认从此函数开始执行??? print This is + self.t_nameif __name__ == __main__:? thread1 = Th(Thread_1)? thread1.start()? #start()函数启动线程,自动执行run()函数;;threading.Thread 类的可继承函数:getName() 获得线程对象名称setName() 设置线程对象名称join() 等待调用的线程结束后再运行之后的命令setDaemon(bool) 阻塞模式, True: 父线程不等待子线程结束, False 等待(默认)isDaemon() 判断子线程是否和父线程一起结束,即 setDaemon() 设置的值isAlive() 判断线程是否在运行;
原创力文档

文档评论(0)