参考:http://www.cnblogs.com/tqsummer/archive/2011/01/25/1944771.html

#!/usr/bin/env python
import sys
import threading import serial
#from threading import Thread
from time import sleep sub_msg_lock = threading.Lock() class thread1_test(threading.Thread): def __init__(self,my_thread_name1,para_for_thread1):
threading.Thread.__init__(self,name = my_thread_name1)
self.thread1_num = para_for_thread1
#self.thread1_stop_flag = False def run(self):#name must is:run
thread1_count =
while thread1_count < :#not self.thread1_stop_flag
print("thread1 started")
sleep(0.5)
self.thread1_num = self.thread1_num +
print("thread1_num:",self.thread1_num)
thread1_count += #def thread1_stop(self):
#self.thread1_stop_flag = True class thread2_test(threading.Thread): def __init__(self,my_thread_name2,para_for_thread2):
threading.Thread.__init__(self,name = my_thread_name2)
self.thread2_num = para_for_thread2
#self.thread2_stop_flag = False def run(self):
thread2_count =
while thread2_count < :#not self.thread1_stop_flag
print("thread2 started")
sleep()
self.thread2_num = self.thread2_num +
print("thread2_num:",self.thread2_num)
thread2_count += def thread2_stop(self):
#thread2_test_instance = thread2_test()
#thread2_name = thread2_test_instance.getName()
#wrong: getName() is method of thread,so must used by instance of thread
#self.thread2_stop_flag = True if __name__ == '__main__':#this is the main thread
thread1 = thread1_test('wang_thread1',)
thread2 = thread2_test('wang_thread2',) thread1.setDaemon(True)
thread2.setDaemon(True)
thread1.start()
thread2.start()
sleep() thread1_name = thread1.getName()
print("thread1_name:",thread1_name)
thread2_name = thread2.getName()
print("thread2_name:",thread2_name) thread1_isdaemon = thread1.isDaemon()
#default Flase: means the son thread will not died with main thread (son has freedom)
#we can through setDaemon(True) to make son threads die with main thread (son can not longer than main) = (main died son must died,son can ealyer died than mian)
print("thread1_isdaemon:",thread1_isdaemon)
thread2_isdaemon = thread2.isDaemon()
print("thread2_isdaemon:",thread2_isdaemon) #thread1.thread1_stop()
#thread2.thread2_stop() thread1_isalive = thread1.isAlive()
print("thread1_isalive:",thread1_isalive)
thread2_isalive = thread2.isAlive()
print("thread2_isalive:",thread2_isalive) #thread1.join()
thread2.join()#main thread wait thread1 and thread2 then main thread go on
#sleep()
print ("mian thread terminate!")
print ("All threads terminate!") 关于线程的方法:

就我个人而言,比较喜欢第二种方式,即创建自己的线程类,必要时重写threading.Thread类的方法,线程的控制可以由自己定制。

threading.Thread类的使用:

1,在自己的线程类的__init__里调用threading.Thread.__init__(self, name = 'threadname') 其中threadname为线程的名字

2, run(),通常需要重写,编写代码实现做需要的功能。

3,getName(),获得线程对象名称

4,setName(),设置线程对象名称

5,start(),启动线程

6,jion([timeout]),等待另一线程结束后再运行。不设置timeout则等待该线程结束,设置timeout后,最多等待线程timeout这么长时间,然后继续运行下面的程序.

7,setDaemon(bool),设置子线程是否随主线程一起结束,必须在start()之前调用。默认为False,即子线程自由与主线程.否则,主线程结束时不管子线程是否结束,都会结束子线程.

8,isDaemon(),判断线程是否随主线程一起结束。

9,isAlive(),检查线程是否在运行中。

此外threading模块本身也提供了很多方法和其他的类,可以帮助我们更好的使用和管理线程。可以参看http://www.python.org/doc/2.5.2/lib/module-threading.html

最新文章

  1. myeclipse 无法启动
  2. MongoDB-常见问题
  3. UVALive 4431 Fruit Weights --floyd,差分约束?
  4. DirectX SDK
  5. Java泛型学习笔记 - (六)泛型的继承
  6. structs2之多文件上传
  7. 实现checkbox组件化(Component)
  8. 1.2 中国象棋将帅问题进一步讨论与扩展:如何用1个变量实现N重循环?[chinese chess]
  9. C++:this指针
  10. weblogic11g 安装——linux 无图形界面
  11. jQuery Ajax无刷新操作
  12. 远程连接mysql
  13. 报错:/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.29.5/UITableView.m:7943解决方法
  14. Http GET、Post方式的请求总结
  15. SQL导入txt以及SQL中的时间格式操作
  16. go语言中sync包和channel机制
  17. scroll、scrollBy和 scrollTo三种方法定位滚动条位置
  18. centos7配置consul
  19. java往MongDB导入大量数据
  20. HDU - 4027 线段树减枝

热门文章

  1. Bootstrap历练实例:表单控件状态(焦点)
  2. C# IsNullOrEmpty与IsNullOrWhiteSpace
  3. Life is short.,You need Python
  4. verilog behavioral modeling ---Block statements
  5. (转)自定义UITabBar
  6. linux中test的意义 又可以表示为[]
  7. Puppet 安装配置
  8. python27 mysqldb window install
  9. 【Spring和SpringMVC】自动扫描注解类的问题
  10. chrome 下载插件包及离线安装 附 Advanced Rest Client 下载