#练习:创建一个线程
from threading import Thread
import time def run(a = None, b = None) :
print a, b
time.sleep(1) t = Thread(target = run, name="wangjing",args = ("this is a", "thread"))
#此时线程是新建状态 print t.getName() #获得线程对象名称
print t.isAlive() #判断线程是否还活着,在start后,在执行完毕前调用isAlive()才会返回True
t.start() #启动线程
#print t.isAlive()
t.join() #主线程等待子线程t执行结束
print "done!" #练习:用继承的方法创建线程
from threading import Thread
import time class MyThread(Thread) :
def __init__(self, a) :
#调用父类的构造方法
super(MyThread, self).__init__()
self.a = a def run(self) :
time.sleep(self.a)
print "sleep :", self.a t1 = MyThread(2)
t2 = MyThread(4)
#多线程的执行是没有顺序,本例主要是增加了较长的等待时间,才看起来有执行顺序
t1.start()
t2.start()
t1.join()
t2.join()
print "done!" #练习:线程
import threading
import time
class timer(threading.Thread):
#The timer class is derived from the class threading.Thread
def __init__(self, num, interval):
threading.Thread.__init__(self)
self.thread_num = num
self.interval = interval
self.thread_stop = False def run(self):
#Overwrite run() method, put what you want the thread do here
while not self.thread_stop:
print 'Thread Object(%d), Time:%s\n' %(self.thread_num, time.ctime())
time.sleep(self.interval)
def stop(self):
self.thread_stop = True
 
def test():
thread1 = timer(1, 1)
thread2 = timer(2, 2)
thread1.start() #执行类里面的run方法
thread2.start()
time.sleep(10) #这里是主线程等待10秒
thread1.stop()
thread2.stop() #停止线程 #这里的停止是主线程调用的
 
if __name__ == '__main__':
test() #练习:线程中再启动一个线程
import threading
import time
def context(tJoin):
print 'in threadContext.'
tJoin.start()
# 将阻塞tContext直到threadJoin终止
tJoin.join()
# tJoin终止后继续执行
print 'out threadContext.' def join1():
print 'in threadJoin.'
time.sleep(1)
print 'out threadJoin.' if __name__=="__main__":
tJoin = threading.Thread(target = join1)
#将线程对象作为参数传入另一个线程中
tContext = threading.Thread(target = context, args = (tJoin,))
tContext.start() #练习:将子线程设置为守护线程
import threading
import time
class MyThread(threading.Thread):
def __init__(self, id):
threading.Thread.__init__(self) def run(self):
time.sleep(5)
print "This is " + self.getName() if __name__ == "__main__":
t1 = MyThread(999)
#t1.setDaemon(True)
# 将子线程设置为守护线程,设置为守护线程后,主线程退出子线程跟着退出,防止子线程挂起
t1.start()
print "I am the father thread."

【Python】多线程-1的更多相关文章

  1. python多线程学习记录

    1.多线程的创建 import threading t = t.theading.Thread(target, args--) t.SetDeamon(True)//设置为守护进程 t.start() ...

  2. python多线程编程

    Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...

  3. Python 多线程教程:并发与并行

    转载于: https://my.oschina.net/leejun2005/blog/398826 在批评Python的讨论中,常常说起Python多线程是多么的难用.还有人对 global int ...

  4. python多线程

    python多线程有两种用法,一种是在函数中使用,一种是放在类中使用 1.在函数中使用 定义空的线程列表 threads=[] 创建线程 t=threading.Thread(target=函数名,a ...

  5. python 多线程就这么简单(转)

    多线程和多进程是什么自行google补脑 对于python 多线程的理解,我花了很长时间,搜索的大部份文章都不够通俗易懂.所以,这里力图用简单的例子,让你对多线程有个初步的认识. 单线程 在好些年前的 ...

  6. python 多线程就这么简单(续)

    之前讲了多线程的一篇博客,感觉讲的意犹未尽,其实,多线程非常有意思.因为我们在使用电脑的过程中无时无刻都在多进程和多线程.我们可以接着之前的例子继续讲.请先看我的上一篇博客. python 多线程就这 ...

  7. python多线程监控指定目录

    import win32file import tempfile import threading import win32con import os dirs=["C:\\WINDOWS\ ...

  8. python多线程ssh爆破

    python多线程ssh爆破 Python 0x01.About 爆弱口令时候写的一个python小脚本,主要功能是实现使用字典多线程爆破ssh,支持ip表导入,字典数据导入. 主要使用到的是pyth ...

  9. 【python,threading】python多线程

    使用多线程的方式 1.  函数式:使用threading模块threading.Thread(e.g target name parameters) import time,threading def ...

  10. <转>Python 多线程的单cpu与cpu上的多线程的区别

    你对Python 多线程有所了解的话.那么你对python 多线程在单cpu意义上的多线程与多cpu上的多线程有着本质的区别,如果你对Python 多线程的相关知识想有更多的了解,你就可以浏览我们的文 ...

随机推荐

  1. js 过滤日期格式

    Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + ...

  2. [NOIP 2015TG D1T3] 斗地主

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  3. JQuary中的FullPage属性的用法

    $(document).ready(function(){ //常用方法    //$.fn.fullpage.moveSectionUp()   //向上滚动一页 //$.fn.fullpage.m ...

  4. weblogic为同一domain下的不同server添加不同参数

    一.背景 今天应用提需求说:现有一应用部署在多个server上,该应用会使用Ddubbo.protocol.port参数指定的端口去启动dubbo,现在想要做到的效果是为每个server的Ddubbo ...

  5. System.gc()和-XX:+DisableExplicitGC启动参数,以及DirectByteBuffer的内存释放

    首先我们修改下JVM的启动参数,重新运行之前博客中的代码.JVM启动参数和测试代码如下: -verbose:gc -XX:+PrintGCDetails -XX:+DisableExplicitGC ...

  6. python javar send

    # -*- coding: utf-8 -*-import jpypeimport os.pathjarpath = os.path.join(os.path.abspath('.'), 'axja' ...

  7. object遍历删除空值

    export function deleteObjEmpty(search = {}) {     for (let i in search) {         search[i] == undef ...

  8. UVa 10891 - Game of Sum 动态规划,博弈 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  9. Animation和Animator 的区别

    此文章转载于极视学堂!!!! ①Animation和Animator 虽然都是控制动画的播放,但是它们的用法和相关语法都是大有不同的. Animation 控制一个动画的播放,而Animator是多个 ...

  10. JDBC:随机生成车牌号,批量插入数据库

    package InsertTest; /* * 单客户端:批量插入 */ import java.sql.Connection; import java.sql.DriverManager; imp ...