# 线程池

# 实例化线程池 ThreadPoolExcutor (推荐cpu_count*(n+1))

# 异步提交任务 submit / map

# 阻塞直到任务完成 shutdown

# 获取子线程的返回值 result

# 使用回调函数 add_done_callback

(1)基本用法:

from concurrent.futures import ThreadPoolExecutor

def func(i):

    print("thread is start",i)

    print("thread is end ")

if __name__ == "__main__":

    p = ThreadPoolExecutor()

    p.submit(func,)  #启动线程

    p.shutdown()  # 相当于join+close

print("主线程")

执行结果:

thread is start 1
thread is end
主线程

(2)返回值 ( 通过对象.result()拿到结果 )

from concurrent.futures import ThreadPoolExecutor
def func(i):
print("thread %s start" % (i))
print("thread %s end" % (i))
return i * "*"
tp = ThreadPoolExecutor(5)
lst = []
for i in range(20):
res = tp.submit(func,i) #返回值也是对象
lst.append(res)
tp.shutdown()
for res in lst:
print(res.result())

执行结果:

thread 0 start
thread 0 end
thread 1 start
thread 2 start
thread 1 endthread 2 end thread 3 start
thread 3 end
thread 4 start
thread 4 end
thread 5 startthread 6 startthread 7 start
thread 7 end
thread 8 start
thread 8 end thread 9 start thread 6 end
thread 10 start
thread 10 end
thread 11 start
thread 11 end
thread 12 start
thread 12 end
thread 13 start
thread 13 end
thread 14 start
thread 14 end
thread 15 startthread 9 endthread 5 end
thread 16 start
thread 16 end
thread 17 start
thread 15 end
thread 18 start
thread 18 end
thread 19 start
thread 17 end thread 19 end *
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
*******************
主线程

(3)map 返回生成器

from concurrent.futures import ThreadPoolExecutor
from threading import current_thread as ct
def func(i):
print("thread",i,ct().ident)
print("thread %s end" % (i))
return i * "*"
tp = ThreadPoolExecutor(5)
res = tp.map(func,range(20))
tp.shutdown()
for i in res: # 生成器
print(i)

执行结果:

thread 0 9336
thread 0 end
thread 1 9336
thread 1 end
thread 2 9336
thread 2 end
thread 3 3348
thread 3 end
thread 4 10116
thread 4 end
thread 5 3348
thread 5 endthread
thread thread7 8 9292
thread 8 end
6 thread 9 9336
thread 6 end
thread9292
thread 9 end
thread 11 10 9336 9292 threadthread 10 end
thread 13 thread3348
thread 11 end
thread 15 thread 7 end
thread 16 12 9292
thread 15 end
14 3348
thread 16 end
10116
thread 14 endthread 17 3348
thread 17 end
thread thread 19 3348
thread 19 end 10068933618 thread 13 end
thread 12 end 9292 thread 18 end *
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
*******************

(19)ThreadPoolExecutor线程池的更多相关文章

  1. 源码剖析ThreadPoolExecutor线程池及阻塞队列

    本文章对ThreadPoolExecutor线程池的底层源码进行分析,线程池如何起到了线程复用.又是如何进行维护我们的线程任务的呢?我们直接进入正题: 首先我们看一下ThreadPoolExecuto ...

  2. 13.ThreadPoolExecutor线程池之submit方法

    jdk1.7.0_79  在上一篇<ThreadPoolExecutor线程池原理及其execute方法>中提到了线程池ThreadPoolExecutor的原理以及它的execute方法 ...

  3. ThreadPoolExecutor 线程池的源码解析

    1.背景介绍 上一篇从整体上介绍了Executor接口,从上一篇我们知道了Executor框架的最顶层实现是ThreadPoolExecutor类,Executors工厂类中提供的newSchedul ...

  4. j.u.c系列(01) ---初探ThreadPoolExecutor线程池

    写在前面 之前探索tomcat7启动的过程中,使用了线程池(ThreadPoolExecutor)的技术 public void createExecutor() { internalExecutor ...

  5. Java并发——ThreadPoolExecutor线程池解析及Executor创建线程常见四种方式

    前言: 在刚学Java并发的时候基本上第一个demo都会写new Thread来创建线程.但是随着学的深入之后发现基本上都是使用线程池来直接获取线程.那么为什么会有这样的情况发生呢? new Thre ...

  6. ThreadPoolExecutor 线程池

    TestThreadPoolExecutorMain package core.test.threadpool; import java.util.concurrent.ArrayBlockingQu ...

  7. 十、自定义ThreadPoolExecutor线程池

    自定义ThreadPoolExecutor线程池 自定义线程池需要遵循的规则 [1]线程池大小的设置 1.计算密集型: 顾名思义就是应用需要非常多的CPU计算资源,在多核CPU时代,我们要让每一个CP ...

  8. Executors、ThreadPoolExecutor线程池讲解

    官方+白话讲解Executors.ThreadPoolExecutor线程池使用 Executors:JDK给提供的线程工具类,静态方法构建线程池服务ExecutorService,也就是Thread ...

  9. SpringBoot项目框架下ThreadPoolExecutor线程池+Queue缓冲队列实现高并发中进行下单业务

    主要是自己在项目中(中小型项目) 有支付下单业务(只是办理VIP,没有涉及到商品库存),目前用户量还没有上来,目前没有出现问题,但是想到如果用户量变大,下单并发量变大,可能会出现一系列的问题,趁着空闲 ...

随机推荐

  1. 终于解决 xUnit.net 测试中无法输出到控制台的问题

    2018-8-2 更新:今天发现在 git bash 中用 dotnet test 运行 xunit 测试可以正常输出到控制台,只是在 PowerShell 与 Windows 命令行中有这个问题. ...

  2. linux vue uwsgi nginx 部署路飞学城 安装 vue

    vue+uwsgi+nginx部署路飞学城 有一天,老男孩的苑日天给我发来了两个神秘代码,听说是和mjj的结晶 超哥将这两个代码,放到了一个网站上,大家可以自行下载 路飞学城django代码#这个代码 ...

  3. Web开发——HTML基础

    文档资料参考: 参考:MDN官网 参考:http://www.runoob.com,W3School 参考:https://developer.mozilla.org/zh-CN/docs/Learn ...

  4. 16.2-uC/OS-III同步 (事件标志组)

    事件标志组 1.当任务要与多个事件同步时可以使用事件标志.若其中的任意一个事件发生时任务被就绪, 叫做逻辑或(OR).若所有的事件都发生时任务被就绪,叫做逻辑与( AND). 2.用户可以创建任意个事 ...

  5. unity 安装破解提示partern not found和tutorials学习

    1.破解安装问题:unity 安装破解提示partern not found 可能和安装了多个版本有关,删除了c下的unity目录也没有解决,试着重新安装也不能破解, 破解的软件的方式是生成lisen ...

  6. Centos安装Oracle及问题处理

    安装Oracle前准备 创建运行oracle数据库的系统用户和用户组 [jonathan@localhost ~]$ su root #切换到root Password: [root@localhos ...

  7. 50.JQ---jQuery 常用小技巧

    1. 禁止右键点击 $(document).ready(function(){ $(document).bind("contextmenu",function(e){ return ...

  8. 检查文件是否被修改或者被破坏工具 md5

    检查文件和对应的md5值是否一致.

  9. bzoj4195(并查集+离散化)

    题目大意:给出n个变量互相的相等或不等关系,求这些关系是否矛盾 思路:把相等的变量加入并查集,不等的查询是否合法 eg:数据很大,离散化(然而我用的是map) #include<stdio.h& ...

  10. EasyUI相关知识点整理

    EasyUI相关知识整理 EasyUI是一种基于jQuery.Angular..Vue和React的用户界面插件集合.easyui为创建现代化,互动,JavaScript应用程序,提供必要的功能.也就 ...