1. # 多线程
  2. import threading
  3. import time
  4. class myThread(threading.Thread):
  5. def __init__(self, threadID, name, counter):
  6. threading.Thread.__init__(self)
  7. self.threadID = threadID
  8. self.name = name
  9. self.counter = counter
  10. def run(self):
  11. print("Starting " + self.name)
  12. # 获得锁,成功获得锁定后返回True
  13. # 可选的timeout参数不填时将一直阻塞直到获得锁定
  14. # 否则超时后将返回False
  15. threadLock.acquire()
  16. print_time(self.name, self.counter, 3)
  17. # 释放锁
  18. threadLock.release()
  19. def print_time(threadName, delay, counter):
  20. while counter:
  21. time.sleep(delay)
  22. print("%s: %s" % (threadName, time.ctime(time.time())))
  23. counter -= 1
  24. threadLock = threading.Lock()
  25. threads = []
  26. # 创建新线程
  27. thread1 = myThread(1, "Thread-1", 1)
  28. thread2 = myThread(2, "Thread-2", 2)
  29. # 开启新线程
  30. thread1.start()
  31. thread2.start()
  32. # 添加线程到线程列表
  33. threads.append(thread1)
  34. threads.append(thread2)
  35. # 等待所有线程完成
  36. for t in threads:
  37. t.join()
  38. print("Exiting Main Thread")
  1. import threadpool
  2. import time
  3. def sayhello(a):
  4. print("hello: " + a)
  5. time.sleep(2)
  6. def main():
  7. global result
  8. seed = ["a", "b", "c"]
  9. start = time.time()
  10. task_pool = threadpool.ThreadPool(5)
  11. requests = threadpool.makeRequests(sayhello, seed)
  12. for req in requests:
  13. task_pool.putRequest(req)
  14. task_pool.wait()
  15. end = time.time()
  16. time_m = end - start
  17. print("time: " + str(time_m))
  18. start1 = time.time()
  19. for each in seed:
  20. sayhello(each)
  21. end1 = time.time()
  22. print("time1: " + str(end1 - start1))
  23. if __name__ == '__main__':
  24. main()
  1. from concurrent.futures import ThreadPoolExecutor
  2. import time
  3. def sayhello(a):
  4. print("hello: " + a)
  5. time.sleep(2)
  6. def main():
  7. seed = ["a", "b", "c"]
  8. start1 = time.time()
  9. for each in seed:
  10. sayhello(each)
  11. end1 = time.time()
  12. print("time1: " + str(end1 - start1))
  13. start2 = time.time()
  14. with ThreadPoolExecutor(3) as executor:
  15. for each in seed:
  16. executor.submit(sayhello, each)
  17. end2 = time.time()
  18. print("time2: " + str(end2 - start2))
  19. start3 = time.time()
  20. with ThreadPoolExecutor(3) as executor1:
  21. executor1.map(sayhello, seed)
  22. end3 = time.time()
  23. print("time3: " + str(end3 - start3))
  24. if __name__ == '__main__':
  25. main()

20.multi_case03的更多相关文章

  1. CSharpGL(20)用unProject和Project实现鼠标拖拽图元

    CSharpGL(20)用unProject和Project实现鼠标拖拽图元 效果图 例如,你可以把Big Dipper这个模型拽成下面这个样子. 配合旋转,还可以继续拖拽成这样. 当然,能拖拽的不只 ...

  2. ABP(现代ASP.NET样板开发框架)系列之20、ABP展现层——动态生成WebApi

    点这里进入ABP系列文章总目录 ABP(现代ASP.NET样板开发框架)系列之20.ABP展现层——动态生成WebApi ABP是“ASP.NET Boilerplate Project (ASP.N ...

  3. 帮我做个APP,给你20万,做不做?

    一.为什么要写这篇文章 前段时间,有个辞职 创业的同事(做法务的)  问我 开发一个 新闻类的APP要多少钱,产品.UI.接口.后台管理页  他们啥都没有,想全部外包. 我 并没有在外包公司做过,也没 ...

  4. ASP.NET MVC5+EF6+EasyUI 后台管理系统(20)-权限管理系统-根据权限获取菜单

    系列目录 不知不觉到20讲,真是漫长的日子,可惜最近工作挺忙,要不可以有更多的时间来更新,多谢大家的一路支持.如果你觉得好,记得帮我点击推荐^-^ 我们在之前已经插入一些真实数据,其中包含了一个用户和 ...

  5. LINQ to SQL语句(20)之存储过程

    在我们编写程序中,往往需要一些存储过程,在LINQ to SQL中怎么使用呢?也许比原来的更简单些.下面我们以NORTHWND.MDF数据库中自带的几个存储过程来理解一下. 1.标量返回 在数据库中, ...

  6. C#开发微信门户及应用(20)-微信企业号的菜单管理

    前面几篇陆续介绍了很多微信企业号的相关操作,企业号和公众号一样都可以自定义菜单,因此他们也可以通过API进行菜单的创建.获取列表.删除的操作,因此本篇继续探讨这个主体,介绍企业号的菜单管理操作. 菜单 ...

  7. 20个非常有用的Java程序片段

    下面是20个非常有用的Java程序片段,希望能对你有用. 1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric strin ...

  8. 20个不可思议的 WebGL 示例和演示

    WebGL 是一项在网页浏览器呈现3D画面的技术,有别于过去需要安装浏览器插件,通过 WebGL 的技术,只需要编写网页代码即可实现3D图像的展示.WebGL 可以为 Canvas 提供硬件3D加速渲 ...

  9. 20款 JavaScript 开发框架推荐给前端开发者

    下面,我们给大家提供了一个用于 HTML5 开发的各种用途的 JavaScript 库列表.这些框架能够给前端开发人员提供更好的功能实现的解决方案.如果你有收藏优秀的框架,也可以在后面的评论中分享给我 ...

随机推荐

  1. prufer序列的性质及相关结论

  2. DOM——事件详解

    事件 事件:触发-响应机制 事件三要素 事件源:触发(被)事件的元素 事件名称: click 点击事件 事件处理程序:事件触发后要执行的代码(函数形式) 事件的基本使用  var box = docu ...

  3. (getElementBy**)与 querySelector(querySelectorAll) 的区别

    1. 通过类似于 document.getElementByTagName('div') 这种方式获取到的类数组,无法通过 forEach 进行遍历(可以通过for循环):而通过document.qu ...

  4. NX二次开发-UFUN获取圆柱的参数UF_MODL_ask_cylinder_parms

    NX11+VS2013 #include <uf.h> #include <uf_modl.h> #include <uf_ui.h> UF_initialize( ...

  5. gvim 安装YouCompleteMe插件

    可以参考:YouCompleteMe#full-installation-guide 可以直接下载: http://pan.baidu.com/s/1dDIq2Al 密码: si5q 确保vim支持p ...

  6. scp 传输下载

    利用scp传输文件 1.从服务器下载文件 scp username@servername:/path/filename /tmp/local_destination 例如scp codinglog@1 ...

  7. LA 3263 /// 欧拉定理 oj21860

    题目大意: n个端点的一笔画 第n个和第1个重合 即一笔画必定是闭合曲线 输出平面被分成的区域数 欧拉定理 V+F-E=2 即 点数+面数-边数=2 (这里的面数包括了外部) #include < ...

  8. callable接口的多线程实现方式

    package com.cxy.juc; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionExce ...

  9. java_函数式编程写法

    package cn.aikang.Test; import org.junit.Test; import java.util.Scanner; import java.util.function.S ...

  10. centos7使用iptables作为防火墙方法

    centos7使用iptables作为防火墙方法查看firewalld状态: systemctl status firewalld将centos7默认的firewalld停止,并将iptables作为 ...