之前用多线程的时候看见了很多文章,比较常用的大概就是join()和setDeamon()了。

先说一下自己对join()的理解吧:

def join(self, timeout=None):
"""Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is
called terminates -- either normally or through an unhandled exception
or until the optional timeout occurs. When the timeout argument is present and not None, it should be a
floating point number specifying a timeout for the operation in seconds
(or fractions thereof). As join() always returns None, you must call
isAlive() after join() to decide whether a timeout happened -- if the
thread is still alive, the join() call timed out. When the timeout argument is not present or None, the operation will
block until the thread terminates. A thread can be join()ed many times. join() raises a RuntimeError if an attempt is made to join the current
thread as that would cause a deadlock. It is also an error to join() a
thread before it has been started and attempts to do so raises the same
exception. """
源码的__doc__其实说的很清楚,join()会阻塞调用该线程的线程,知道该线程结束(This blocks the calling thread
until the thread whose join() method is called terminates)。
注意点:
#coding:utf-8
import threading
import time def action(arg):
time.sleep(1)
print 'sub thread start!the thread name is:%s ' % threading.currentThread().getName()
print 'the arg is:%s ' %arg
time.sleep(1) #不正确写法,会导致多线程顺序执行,失去了多线程的意义
for i in xrange(4):
t =threading.Thread(target=action,args=(i,))
t.setDaemon(True)
t.start()
t.join() #正确写法
thread_list = [] #线程存放列表
for i in xrange(4):
t =threading.Thread(target=action,args=(i,))
t.setDaemon(True)
thread_list.append(t) for t in thread_list:
t.start() for t in thread_list:
t.join() print 'main_thread end!'

下面再说一下setDeamon()吧:

其实主线程并不会结束setDeamon(True)的线程,而是当主线程执行完毕之后不会再去关注setDeamon(True)的线程。

所以setDeamon(True)的线程的结果是我们无法获取到的,类似于爱咋咋地?不管你输出什么,我都不看,主线程跑

完就结束整个python process。

而setDeamon(False)的线程会一直受到主线程的关注,就算主线程跑完了也会等setDeamon(False)的线程跑完然后

再结束整个python process。

所以说,就算setDeamon(True)的线程在主线程之后跑完,但如果在setDeamon(False)的线程之前跑完的话,也是会

输出结果的,而不是被所谓的主线程结束就杀死setDeamon(False)的线程。

附上我的调试代码,

# -*- coding:utf-8 -*-

import threading
import time stime = time.time() def action(arg):
time.sleep(1)
print ('the arg is:%s,time is %s ' % (arg, time.time())) def action1(arg):
time.sleep(1)
print ('the arg is:%s,time is %s ' % (arg, time.time())) thread_list = [] # 线程存放列表
for i in [777, 888]:
t = threading.Thread(target=action, args=(i,))
t.setDaemon(False)
thread_list.append(t)
for i in range(10):
t = threading.Thread(target=action1, args=(i,))
t.setDaemon(True)
thread_list.append(t) for t in thread_list:
t.start() print('**************')
etime = time.time()
print('time cost is %s' % (etime - stime))

python threading模块中的join()方法和setDeamon()方法的一些理解的更多相关文章

  1. Java8新特性(一)_interface中的static方法和default方法

    什么要单独写个Java8新特性,一个原因是我目前所在的公司用的是jdk8,并且框架中用了大量的Java8的新特性,如上篇文章写到的stream方法进行过滤map集合.stream方法就是接口Colle ...

  2. JS中的call()方法和apply()方法用法总结

    原文引自:https://blog.csdn.net/ganyingxie123456/article/details/70855586 最近又遇到了JacvaScript中的call()方法和app ...

  3. Hibernate中Session.get()方法和load()方法的详细比较

    一.get方法和load方法的简易理解  (1)get()方法直接返回实体类,如果查不到数据则返回null.load()会返回一个实体代理对象(当前这个对象可以自动转化为实体对象),但当代理对象被调用 ...

  4. js中的splice方法和slice方法简单总结

    slice:是截取用的 splice:是做删除 插入 替换用的 slice(start,end): 参数: start:开始位置的索引 end:结束位置的索引(但不包含该索引位置的元素) 例如: va ...

  5. JS中的call()方法和apply()方法用法总结(挺好 转载下)

    最近又遇到了JacvaScript中的call()方法和apply()方法,而在某些时候这两个方法还确实是十分重要的,那么就让我总结这两个方法的使用和区别吧. 1. 每个函数都包含两个非继承而来的方法 ...

  6. TP框架中的A方法和R方法

    ThinkPHP 跨模块调用操作方法(A方法与R方法) 跨模块调用操作方法 前面说了可以使用 $this 来调用当前模块内的方法,但实际情况中还经常会在当前模块调用其他模块的方法.ThinkPHP 内 ...

  7. Mapper类/Reducer类中的setup方法和cleanup方法以及run方法的介绍

    在hadoop的源码中,基类Mapper类和Reducer类中都是只包含四个方法:setup方法,cleanup方法,run方法,map方法.如下所示: 其方法的调用方式是在run方法中,如下所示: ...

  8. java 中的set方法和get方法的理解

    get的意思是获取,set的意思是设置. get方法和set方法是实现类的封装访问的很好的工具. 当类中的变量设为private 时,他的意思就是说,只能通过自身和子类的访问,但是对于别的其他的类来说 ...

  9. java8新特性:interface中的static方法和default方法

    java8中接口有两个新特性,一个是静态方法,一个是默认方法. static方法 java8中为接口新增了一项功能:定义一个或者多个静态方法. 定义用法和普通的static方法一样: public i ...

随机推荐

  1. android开发_ViewGroup(组视图)-- 五大布局

    view组--ViewGroup(组视图) ViewGroup的作用:在view中添加子控件.ViewGroup的5个子类,就是五大布局: (1) LinearLayout  线性布局(常用) (2) ...

  2. javascript自定义一个全类型读取的函数

    我爱撸码,撸码使我感到快乐!大家好,我是Counter.因为我们知道,在JavaScript中有自带的方法可以读取类型,但是不很全面,今天来分享下如何自己定义一个函数,将所有传入参数的类型给打印出来, ...

  3. jmeter接口自动化测试

    一.正常单个接口 1.自定义变量设置服务器地址ip和端口 2.可以正则表达式提取取出token值设置为请求头里 如图 二.接口请求参数涉及取参(单个或多值) 提取多个值参数,用Json提取器可以直接提 ...

  4. lambda 委托 匿名方法

    委托: delegate是C#中的一种类型,它实际上是一个能够持有对某个方法的引用的类.与其它的类不同,delegate类能够拥有一个签名(signature),并且它只能持有与它的签名相匹配的方法的 ...

  5. Go语言库之strconv包(转载自--http://blog.csdn.net/alvine008/article/details/51283189)

    golang strconv.ParseInt 是将字符串转换为数字的函数 func ParseInt(s string, base int, bitSize int) (i int64, err e ...

  6. 安装PyCharm开发工具

    1.进入PyCharm官网 http://www.jetbrains.com/pycharm/ 2.点击现在下载 3.选择windows版本 4.打开安装程序 5.下一步,选择安装路径,安装 6.安装 ...

  7. Jenkins部署的时候报错

    拿了一个最简单的,好不容易maven开始跑了 最终给我报错了 [INFO] -------------------------------------------------------------- ...

  8. 常看本地是否安装Git和maven工具

    打开cmd命令行工具: 查看git where git C:\Users\jasqia>where gitC:\Program Files\Git\cmd\git.exe 安装maven后需要到 ...

  9. js及jsp区别

  10. ggplot的boxplot添加显著性 | Add P-values and Significance Levels to ggplots | 方差分析

    参考:Add P-values and Significance Levels toggplots 多组比较,挑选感兴趣的显示显著性. data("ToothGrowth") he ...