python vs java Threadpool
python 实现threadpool线程池管理:
- from concurrent.futures import ThreadPoolExecutor as te
- from concurrent.futures import ProcessPoolExecutor as pe
- from concurrent.futures import wait
- from concurrent.futures import FIRST_COMPLETED, ALL_COMPLETED, as_completed
- import time
- import os
- import logging
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s -\n %(message)s')
- Executor_Service = te(max_workers=5) # create a pool contains five workers executor service
- Executors_Service_P = pe(max_workers=os.cpu_count()) # create a pool contains max cpu count Process executor service
- def test_fan(*k):
- """use *k 解包参数"""
- # logging.info(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " ->get result is:" + str(k)+"pid is %s"%os.getpid())
- time.sleep(sum(k) / 10)
- return k
- if __name__ == '__main__':
- print("cur computer max cpu number is %s" % os.cpu_count())
- futures = []
- for i in range(10):
- list_test = [i, i + 1, i + 2]
- future = Executor_Service.submit(test_fan, *list_test)
- futures.append(future)
- Executor_Service.shutdown() # shutdown pool
- results = []
- for f in futures:
- if f.done(): # bool type
- results.append(f.result())
- print(f"get result of all : {results}")
- # part2 演示ALL_COMPLETED
- wait(futures, timeout=10, return_when=ALL_COMPLETED)
- # 问题:上面虽然提供了判断任务是否结束的方法,但是不能在主线程中一直判断啊\
- # TODO如何实现一有任务结束就立马返回结果呢:借助as_completed
- for f in as_completed(futures, timeout=10):
- print(f.result())
java 实现线程池管理:
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class MyExecuter implements Runnable{
- private int i=0;
- @Override
- public void run(){
- while (i<10) {
- SimpleDateFormat strf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
- String d = strf.format(new Date());// new Date()为获取当前系统时间
- System.out.println(d+" "+Thread.currentThread().getName());
- i++;
- }
- }
- public static void main(String[] args) {
- ExecutorService pool = Executors.newFixedThreadPool(5);
- for (int i = 0; i <10; i++) {
- pool.submit(new MyExecuter());
- }
- pool.shutdown(); //shutdown all task wait all task finish ,not longer recv new task ,shutdownNow 立即关闭线程池
- }
- }
python vs java Threadpool的更多相关文章
- Golang、Php、Python、Java基于Thrift0.9.1实现跨语言调用
目录: 一.什么是Thrift? 1) Thrift内部框架一瞥 2) 支持的数据传输格式.数据传输方式和服务模型 3) Thrift IDL 二.Thrift的官方网站在哪里? 三.在哪里下载?需要 ...
- paip.判断文件是否存在uapi python php java c#
paip.判断文件是否存在uapi python php java c# ==========uapi file_exists exists() 面向对象风格: File.Exists 作者: 老哇 ...
- paip.web数据绑定 下拉框的api设计 选择框 uapi python .net java swing jsf总结
paip.web数据绑定 下拉框的api设计 选择框 uapi python .net java swing jsf总结 ====总结: 数据绑定下拉框,Uapi 1.最好的是默认绑定..Map(k ...
- Java threadpool机制深入分析
简介 在前面的一篇文章里我对java threadpool的几种基本应用方法做了个总结.Java的线程池针对不同应用的场景,主要有固定长度类型.可变长度类型以及定时执行等几种.针对这几种类型的创建,j ...
- MongoDB的账户与权限管理及在Python与Java中的登陆
本文主要介绍了MongoDB的账户新建,权限管理(简单的),以及在Python,Java和默认客户端中的登陆. 默认的MongoDB是没有账户权限管理的,也就是说,不需要密码即可登陆,即可拥有读写的权 ...
- 谈谈Python、Java与AI
Python好像天生是为AI而生的,随着AI的火热,特别是用Python写的TensorFlow越来越火,Python的热度越来越高,就像当年Java就是随着互联网火起来的感觉.在我的工作中,Pyth ...
- [翻译] 比较 Node.js,Python,Java,C# 和 Go 的 AWS Lambda 性能
[翻译] 比较 Node.js,Python,Java,C# 和 Go 的 AWS Lambda 性能 原文: Comparing AWS Lambda performance of Node.js, ...
- Python和Java的硬盘夜话
这是一个程序员的电脑硬盘,在一个叫做"学习"的目录下曾经生活着两个小程序,一个叫做Hello.java,即Java小子:另外一个叫做hello.c ,也就是C老头儿. C老头儿的命 ...
- 将来会是Python、Java、Golang三足鼎立的局面吗?
甲:听说最近java跌落神坛,python称霸武林了,你知道吗? 乙:不是吧,我前几天看python怎么还是第三? 丙:你们都在扯蛋,python在2018年就已经是最好的语言了! 乙:不可能吧? 甲 ...
随机推荐
- 重新安装python后,原来在虚拟环境里的django项目启动报错:dyld: Library not loaded: @executable_path/../.Python Referenced from: /Users/mac/.virtualenvs/WYGBlog-env/bin/python Reason: image not found
因为当你创建一个虚拟环境的时候,一些软链接创建到原来的python上. 当用Homebrew更新python后,原来软连接对应的python已经不存在了. 因此需要把软链接指向新的python. 解决 ...
- 记录zabbix4.0升级4.2
系统环境 [root@localhost ~]# cat /etc/redhat-release CentOS release 6.9 (Final) 官方网站 官方文档升级其实很简单如果 ...
- 记录一个解决IOS极光推送解决问题方法的网址csdn
https://blog.csdn.net/Three_Zhang/article/details/54667258
- Excel 2016双击文件打开后是空白,再次双击才能打开(或者通过文件,打开才能打开)
问题描述: 直接双击excel文件打开后是空白的,几乎所有功能都无法使用.但是再次双击该文件能够打开,或者通过文件 --> 打开的方式才能打开. 虽说能够打开文件,但是对于咱们这种追求完美的人来 ...
- 题解【CF1311F Moving Points】
\[ \texttt{Preface} \] 赛时,把 " 任意时刻 " 理解成 " 整数时刻 " 了,看起来一脸不可做的亚子,还各种推式子. 话说我为什么觉得 ...
- jQuery---事件解绑与事件触发
事件解绑与事件触发 $("p").off("click"); $("#btn").on("click", functio ...
- CVE-2020-1938/CNVD-2020-10487 幽灵猫漏洞
漏洞描述(后期跟进漏洞分析) Tomcat是由Apache软件基金会属下Jakarta项目开发的Servlet容器,按照Sun Microsystems提供的技术规范,实现了对Servlet和Java ...
- 纪中18日c组模拟赛
T2 GMOJ2127. 电子表格 (File IO): input:excel.in output:excel.out 时间限制: 1000 ms 空间限制: 262144 KB 具体限制 ...
- C++使用taskkill 命令强制结束进程
一:查看 taskkill 命令和参数的方法 window系统下,快捷键win + R 打开运行 ,输入cmd回车,在 cmd 里面输入: taskkill /? 二:语法: taskkill [/ ...
- 错误 CS8107 C# 7.0 中不支持功能“xxxxxx”。请使用 7.1 或更高的语言版本。
解决方法:项目右键属性 —> 生成 —> 找到最下面的高级按钮,点击高级按钮 —> 常规 —> 语言版本 —> 选择 C#最新次要版本,或者比当前版本更高的版本即可,点击 ...