Python并发编程-concurrent包

                                 作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.concurrent.futures包概述

  3.2版本引入的模块。 

  异步并行任务编程模块,提供一个高级的异步可执行的便利接口。 

  提供了2个池执行器:
    ThreadPoolExecutor 异步调用的线程池的Executor
    ProcessPoolExecutor 异步调用的进程池的Executor

二.ThreadPoolExecutor对象

1>.首先需要定义一个池的执行器对象,Executor类子类对象。 

ThreadPoolExecutor(max_workers=1)
  池中至多创建max_workers个线程的池来同时异步执行,返回Executor实例
submit(fn, *args, **kwargs)
  提交执行的函数及其参数,返回Future类的实例

shutdown(wait=True)
  清理池

2>.Future类

done()
  如果调用被成功的取消或者执行完成,返回True

cancelled()
  如果调用被成功的取消,返回True

running()
  如果正在运行且不能被取消,返回True

cancel()
  尝试取消调用。如果已经执行且不能取消返回False,否则返回True

result(timeout=None)
  取返回的结果,timeout为None,一直等待返回;timeout设置到期,抛出"concurrent.futures.TimeoutError"异常

exception(timeout=None)
  取返回的异常,timeout为None,一直等待返回;timeout设置到期,抛出"concurrent.futures.TimeoutError"异常

3>.ThreadPoolExecutor案例实战

 #!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie import threading
from concurrent import futures
import logging
import time # 输出格式定义
FORMAT = '%(asctime)-15s\t [%(processName)s:%(threadName)s, %(process)d:%(thread)8d] %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT) def worker(n):
logging.info('begin to work-{}'.format(n))
time.sleep(5)
logging.info('finished {}'.format(n)) """
线程池并不着急创建线程,用的时候创建,创建后不清除,复用
线程池一旦创建了线程,就不需要频繁清除
"""
executer = futures.ThreadPoolExecutor(max_workers=3) #创建线程池,池容量为3 fs = []
for i in range(3):
future = executer.submit(worker, i)
fs.append(future) for i in range(3, 6):
future = executer.submit(worker, i)
fs.append(future) while True:
time.sleep(2)
logging.info(threading.enumerate()) flag = True
for f in fs: # 判断是否还有未完成的任务
logging.info(f.done())
flag = flag and f.done()
# if not flag: # 注释了这个if,输出的日志看的清楚些
# break
print("-" * 30) if flag:
executer.shutdown()
logging.info(threading.enumerate())
break
2019-12-04 19:11:33,108     [MainProcess:ThreadPoolExecutor-0_0, 16684:123145346408448] begin to work-0
2019-12-04 19:11:33,108 [MainProcess:ThreadPoolExecutor-0_1, 16684:123145351663616] begin to work-1
2019-12-04 19:11:33,109 [MainProcess:ThreadPoolExecutor-0_2, 16684:123145356918784] begin to work-2
------------------------------
2019-12-04 19:11:35,113 [MainProcess:MainThread, 16684:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(ThreadPoolExecutor-0_0, started daemon 123145346408448)>, <Thread(ThreadPoolExecutor-0_1, started daemon 123145351663616)>, <Thread(ThreadPoolExecutor-0_2, started daemon 123145356918784)>]
2019-12-04 19:11:35,113 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:35,113 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:35,113 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:35,113 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:35,113 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:35,113 [MainProcess:MainThread, 16684:140736847635392] False
------------------------------
2019-12-04 19:11:37,114 [MainProcess:MainThread, 16684:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(ThreadPoolExecutor-0_0, started daemon 123145346408448)>, <Thread(ThreadPoolExecutor-0_1, started daemon 123145351663616)>, <Thread(ThreadPoolExecutor-0_2, started daemon 123145356918784)>]
2019-12-04 19:11:37,114 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:37,114 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:37,114 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:37,114 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:37,114 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:37,114 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:38,111 [MainProcess:ThreadPoolExecutor-0_0, 16684:123145346408448] finished 0
2019-12-04 19:11:38,111 [MainProcess:ThreadPoolExecutor-0_0, 16684:123145346408448] begin to work-3
2019-12-04 19:11:38,111 [MainProcess:ThreadPoolExecutor-0_1, 16684:123145351663616] finished 1
2019-12-04 19:11:38,112 [MainProcess:ThreadPoolExecutor-0_1, 16684:123145351663616] begin to work-4
2019-12-04 19:11:38,112 [MainProcess:ThreadPoolExecutor-0_2, 16684:123145356918784] finished 2
2019-12-04 19:11:38,112 [MainProcess:ThreadPoolExecutor-0_2, 16684:123145356918784] begin to work-5
------------------------------
2019-12-04 19:11:39,115 [MainProcess:MainThread, 16684:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(ThreadPoolExecutor-0_0, started daemon 123145346408448)>, <Thread(ThreadPoolExecutor-0_1, started daemon 123145351663616)>, <Thread(ThreadPoolExecutor-0_2, started daemon 123145356918784)>]
2019-12-04 19:11:39,115 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:39,115 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:39,115 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:39,115 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:39,116 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:39,116 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:41,120 [MainProcess:MainThread, 16684:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(ThreadPoolExecutor-0_0, started daemon 123145346408448)>, <Thread(ThreadPoolExecutor-0_1, started daemon 123145351663616)>, <Thread(ThreadPoolExecutor-0_2, started daemon 123145356918784)>]
2019-12-04 19:11:41,120 [MainProcess:MainThread, 16684:140736847635392] True
------------------------------
2019-12-04 19:11:41,120 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:41,120 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:41,120 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:41,120 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:41,120 [MainProcess:MainThread, 16684:140736847635392] False
2019-12-04 19:11:43,116 [MainProcess:ThreadPoolExecutor-0_2, 16684:123145356918784] finished 5
2019-12-04 19:11:43,116 [MainProcess:ThreadPoolExecutor-0_0, 16684:123145346408448] finished 3
2019-12-04 19:11:43,116 [MainProcess:ThreadPoolExecutor-0_1, 16684:123145351663616] finished 4
2019-12-04 19:11:43,122 [MainProcess:MainThread, 16684:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(ThreadPoolExecutor-0_0, started daemon 123145346408448)>, <Thread(ThreadPoolExecutor-0_1, started daemon 123145351663616)>, <Thread(ThreadPoolExecutor-0_2, started daemon 123145356918784)>]
2019-12-04 19:11:43,122 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:43,122 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:43,122 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:43,122 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:43,122 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:43,122 [MainProcess:MainThread, 16684:140736847635392] True
2019-12-04 19:11:43,123 [MainProcess:MainThread, 16684:140736847635392] [<_MainThread(MainThread, started 140736847635392)>]
------------------------------

以上代码执行结果戳这里

三.ProcessPoolExecutor对象

 #!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie import threading
from concurrent import futures
import logging
import time
# 输出格式定义
FORMAT = '%(asctime)-15s\t [%(processName)s:%(threadName)s, %(process)d:%(thread)8d] %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT) def worker(n):
logging.info('begin to work-{}'.format(n))
time.sleep(5)
logging.info('finished {}'.format(n)) if __name__ == '__main__':
"""
方法一样。就是使用多进程完成。
"""
executer = futures.ProcessPoolExecutor(max_workers=3) # 创建进程池,池容量为3 fs = []
for i in range(3):
future = executer.submit(worker, i)
fs.append(future) for i in range(3, 6):
future = executer.submit(worker, i)
fs.append(future) while True:
time.sleep(2)
logging.info(threading.enumerate()) flag = True
for f in fs: # 判断是否还有未完成的任务
logging.info(f.done())
flag = flag and f.done()
#if not flag: # 注释了这个if,输出的日志看的清楚些
# break
print('-' * 30) if flag:
executer.shutdown() # 清理池。除非不用了,否则不用频繁清理池
logging.info(threading.enumerate()) # 多进程时看主线程已没有必要了
break
2019-12-04 19:17:23,219     [Process-1:MainThread, 16714:140736847635392] begin to work-0
2019-12-04 19:17:23,219 [Process-2:MainThread, 16715:140736847635392] begin to work-1
2019-12-04 19:17:23,219 [Process-3:MainThread, 16716:140736847635392] begin to work-2
------------------------------
2019-12-04 19:17:25,222 [MainProcess:MainThread, 16713:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(Thread-1, started daemon 123145351159808)>, <Thread(QueueFeederThread, started daemon 123145356414976)>]
2019-12-04 19:17:25,223 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:25,223 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:25,223 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:25,223 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:25,223 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:25,223 [MainProcess:MainThread, 16713:140736847635392] False
------------------------------
2019-12-04 19:17:27,227 [MainProcess:MainThread, 16713:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(Thread-1, started daemon 123145351159808)>, <Thread(QueueFeederThread, started daemon 123145356414976)>]
2019-12-04 19:17:27,227 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:27,227 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:27,227 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:27,227 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:27,227 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:27,227 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:28,220 [Process-2:MainThread, 16715:140736847635392] finished 1
2019-12-04 19:17:28,220 [Process-1:MainThread, 16714:140736847635392] finished 0
2019-12-04 19:17:28,221 [Process-3:MainThread, 16716:140736847635392] finished 2
2019-12-04 19:17:28,221 [Process-2:MainThread, 16715:140736847635392] begin to work-3
2019-12-04 19:17:28,222 [Process-1:MainThread, 16714:140736847635392] begin to work-4
2019-12-04 19:17:28,222 [Process-3:MainThread, 16716:140736847635392] begin to work-5
2019-12-04 19:17:29,232 [MainProcess:MainThread, 16713:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(Thread-1, started daemon 123145351159808)>, <Thread(QueueFeederThread, started daemon 123145356414976)>]
2019-12-04 19:17:29,233 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:29,233 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:29,233 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:29,233 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:29,233 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:29,233 [MainProcess:MainThread, 16713:140736847635392] False
------------------------------
2019-12-04 19:17:31,237 [MainProcess:MainThread, 16713:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(Thread-1, started daemon 123145351159808)>, <Thread(QueueFeederThread, started daemon 123145356414976)>]
2019-12-04 19:17:31,238 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:31,238 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:31,238 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:31,238 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:31,238 [MainProcess:MainThread, 16713:140736847635392] False
2019-12-04 19:17:31,238 [MainProcess:MainThread, 16713:140736847635392] False
------------------------------
2019-12-04 19:17:33,223 [Process-2:MainThread, 16715:140736847635392] finished 3
2019-12-04 19:17:33,223 [Process-1:MainThread, 16714:140736847635392] finished 4
2019-12-04 19:17:33,224 [Process-3:MainThread, 16716:140736847635392] finished 5
------------------------------
2019-12-04 19:17:33,240 [MainProcess:MainThread, 16713:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(Thread-1, started daemon 123145351159808)>, <Thread(QueueFeederThread, started daemon 123145356414976)>]
2019-12-04 19:17:33,240 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:33,240 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:33,240 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:33,240 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:33,241 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:33,241 [MainProcess:MainThread, 16713:140736847635392] True
2019-12-04 19:17:33,245 [MainProcess:MainThread, 16713:140736847635392] [<_MainThread(MainThread, started 140736847635392)>]

以上代码执行结果戳这里

四.支持上下文管理

 #!/usr/bin/env python
#_*_conding:utf-8_*_
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie """
concurrent.futures.ProcessPoolExecutor继承自concurrent.futures._base.Executor,而父类有 __enter__ 、__exit__方法,支持上下文管理。可以使用with语句。 __exit__方法本质还是调用的shutdown(wait=True),就是一直阻塞到所有运行的任务完成 使用方法
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(pow, 323, 1235)
print(future.result())
""" import threading
from concurrent import futures
import logging
import time # 输出格式定义
FORMAT = '%(asctime)-15s\t [%(processName)s:%(threadName)s, %(process)d:%(thread)8d] %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT) def worker(n):
logging.info('begin to work-{}'.format(n))
time.sleep(5)
logging.info('finished {}'.format(n)) if __name__ == '__main__':
executer = futures.ProcessPoolExecutor(max_workers=3) # 创建进程池,池容量为3 with executer: #使用上下文改造进程池的例子,增加返回计算的结果
fs = []
for i in range(3):
future = executer.submit(worker, i)
fs.append(future) for i in range(3, 6):
future = executer.submit(worker, i)
fs.append(future) while True:
time.sleep(2)
logging.info(threading.enumerate())
flag = True
for f in fs: # 判断是否还有未完成的任务
logging.info(f.done())
flag = flag and f.done()
#if not flag: # 注释了这个if,输出的日志看的清楚些
# break print('-' * 30)
if flag:
break
#executer.shutdown() # 上下文清理了资源
logging.info('===end====')
logging.info(threading.enumerate()) # 多进程时看主线程已没有必要了
2019-12-04 19:25:33,520     [Process-1:MainThread, 16781:140736847635392] begin to work-0
2019-12-04 19:25:33,520 [Process-2:MainThread, 16782:140736847635392] begin to work-1
2019-12-04 19:25:33,521 [Process-3:MainThread, 16783:140736847635392] begin to work-2
2019-12-04 19:25:35,522 [MainProcess:MainThread, 16780:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(Thread-1, started daemon 123145557798912)>, <Thread(QueueFeederThread, started daemon 123145563054080)>]
2019-12-04 19:25:35,523 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:35,523 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:35,523 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:35,523 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:35,523 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:35,523 [MainProcess:MainThread, 16780:140736847635392] False
------------------------------
------------------------------
2019-12-04 19:25:37,527 [MainProcess:MainThread, 16780:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(Thread-1, started daemon 123145557798912)>, <Thread(QueueFeederThread, started daemon 123145563054080)>]
2019-12-04 19:25:37,527 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:37,527 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:37,527 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:37,527 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:37,527 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:37,527 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:38,521 [Process-1:MainThread, 16781:140736847635392] finished 0
2019-12-04 19:25:38,522 [Process-1:MainThread, 16781:140736847635392] begin to work-3
2019-12-04 19:25:38,522 [Process-2:MainThread, 16782:140736847635392] finished 1
2019-12-04 19:25:38,522 [Process-3:MainThread, 16783:140736847635392] finished 2
2019-12-04 19:25:38,523 [Process-2:MainThread, 16782:140736847635392] begin to work-4
2019-12-04 19:25:38,524 [Process-3:MainThread, 16783:140736847635392] begin to work-5
------------------------------
2019-12-04 19:25:39,532 [MainProcess:MainThread, 16780:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(Thread-1, started daemon 123145557798912)>, <Thread(QueueFeederThread, started daemon 123145563054080)>]
2019-12-04 19:25:39,532 [MainProcess:MainThread, 16780:140736847635392] True
2019-12-04 19:25:39,532 [MainProcess:MainThread, 16780:140736847635392] True
2019-12-04 19:25:39,532 [MainProcess:MainThread, 16780:140736847635392] True
2019-12-04 19:25:39,532 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:39,532 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:39,533 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:41,536 [MainProcess:MainThread, 16780:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(Thread-1, started daemon 123145557798912)>, <Thread(QueueFeederThread, started daemon 123145563054080)>]
2019-12-04 19:25:41,536 [MainProcess:MainThread, 16780:140736847635392] True
------------------------------
2019-12-04 19:25:41,536 [MainProcess:MainThread, 16780:140736847635392] True
2019-12-04 19:25:41,536 [MainProcess:MainThread, 16780:140736847635392] True
2019-12-04 19:25:41,536 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:41,536 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:41,536 [MainProcess:MainThread, 16780:140736847635392] False
2019-12-04 19:25:43,522 [Process-1:MainThread, 16781:140736847635392] finished 3
2019-12-04 19:25:43,524 [Process-2:MainThread, 16782:140736847635392] finished 4
2019-12-04 19:25:43,524 [Process-3:MainThread, 16783:140736847635392] finished 5
2019-12-04 19:25:43,537 [MainProcess:MainThread, 16780:140736847635392] [<_MainThread(MainThread, started 140736847635392)>, <Thread(Thread-1, started daemon 123145557798912)>, <Thread(QueueFeederThread, started daemon 123145563054080)>]
2019-12-04 19:25:43,538 [MainProcess:MainThread, 16780:140736847635392] True
2019-12-04 19:25:43,538 [MainProcess:MainThread, 16780:140736847635392] True
2019-12-04 19:25:43,538 [MainProcess:MainThread, 16780:140736847635392] True
2019-12-04 19:25:43,538 [MainProcess:MainThread, 16780:140736847635392] True
2019-12-04 19:25:43,538 [MainProcess:MainThread, 16780:140736847635392] True
2019-12-04 19:25:43,538 [MainProcess:MainThread, 16780:140736847635392] True
------------------------------
2019-12-04 19:25:43,541 [MainProcess:MainThread, 16780:140736847635392] ===end====
2019-12-04 19:25:43,542 [MainProcess:MainThread, 16780:140736847635392] [<_MainThread(MainThread, started 140736847635392)>]

以上代码执行结果戳这里

五.总结

  该库统一了线程池、进程池调用,简化了编程。 

  是Python简单的思想哲学的体现。

  唯一的缺点:无法设置线程名称。但这都不值一提。

Python并发编程-concurrent包的更多相关文章

  1. 深入理解java:2.3.4. 并发编程concurrent包 之容器ConcurrentLinkedQueue(非阻塞的并发队列---循环CAS)

    1.    引言 在并发编程中我们有时候需要使用线程安全的队列. 如果我们要实现一个线程安全的队列有两种实现方式:一种是使用阻塞算法,另一种是使用非阻塞算法. 使用阻塞算法的队列可以用一个锁(入队和出 ...

  2. 深入理解java:2.3.6. 并发编程concurrent包 之管理类---线程池

    我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了,这样频繁创建线程就会大大降低系统的效率,因为频繁 ...

  3. 深入理解java:2.3.2. 并发编程concurrent包 之重入锁/读写锁/条件锁

    重入锁 Java中的重入锁(即ReentrantLock)   与JVM内置锁(即synchronized)一样,是一种排它锁. ReentrantLock提供了多样化的同步,比如有时间限制的同步(定 ...

  4. 深入理解java:2.3.1. 并发编程concurrent包 之Atomic原子操作(循环CAS)

    java中,可能有一些场景,操作非常简单,但是容易存在并发问题,比如i++, 此时,如果依赖锁机制,可能带来性能损耗等问题, 于是,如何更加简单的实现原子性操作,就成为java中需要面对的一个问题. ...

  5. 深入理解java:2.3.3. 并发编程concurrent包 之容器ConcurrentHashMap

    线程不安全的HashMap 因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap. 效率低下的HashTable容器 H ...

  6. 深入理解java:2.3.5. 并发编程concurrent包 之容器BlockingQueue(阻塞队列)

    1. 什么是阻塞队列? 阻塞队列(BlockingQueue)是一个支持两个附加操作的队列. 这两个附加的操作是:在队列为空时,获取元素的线程会等待队列变为非空. 当队列满时,存储元素的线程会等待队列 ...

  7. 《转载》Python并发编程之线程池/进程池--concurrent.futures模块

    本文转载自Python并发编程之线程池/进程池--concurrent.futures模块 一.关于concurrent.futures模块 Python标准库为我们提供了threading和mult ...

  8. 快速了解Python并发编程的工程实现(上)

    关于我 一个有思想的程序猿,终身学习实践者,目前在一个创业团队任team lead,技术栈涉及Android.Python.Java和Go,这个也是我们团队的主要技术栈. Github:https:/ ...

  9. Python 并发编程(下)

    Python 并发编程(下) 课程目标:掌握多进程开发的相关知识点并初步认识协程. 今日概要: 多进程开发 进程之间数据共享 进程锁 进程池 协程 1. 多进程开发 进程是计算机中资源分配的最小单元: ...

随机推荐

  1. DLL注入实践

    Windows系统大量使用dll作为组件复用,应用程序也会通过dll实现功能模块的拆分.DLL注入技术是向一个正在运行的进程插入自有DLL的过程. Window下的代码注入 常见的Windows代码注 ...

  2. advanceInstaller制作中文安装界面

    AdvanceInstaller15.7 ,上述画框选择.

  3. python办公自动化(一)PPTX

    简介: python-pptx是python处理PPT的一个库,注重的是读和写,无法导出,没有渲染功能. 办公自动化,说的是大了一点,但是最常见的office三件套,word,excel,ppt.这还 ...

  4. Laradock中文文档

    文档地址:https://laradock.linganmin.cn

  5. Linux 就该这么学 CH04 VIM编辑器和Shell命令脚本

    0 概述 1 Vim编辑器 在linux 中一切都是文件,而配置一个服务就是修改其配置文件的参数. vim 编辑器有三种模式:命令模式,末行模式和编辑模式. 命令模式:控制光标移动,对文件进行操作. ...

  6. 各手机PC品牌投屏功能连接方法

    一.iOS终端(iPhone/iPad)无线投屏: 1.将iPhone或iPad与必捷会议盒子连接至同一路由器: 2.滑动iPhone/iPad的屏幕,调出Airplay功能,选择需要投屏的主机,开始 ...

  7. 在ensp上配置RIPv2的认证

    原理 实验模拟 实验拓扑 测试连通性 搭建rip网络 查看路由表 R3模拟攻击首先把在自己的的ip通告网段 配置完成后查看一下路由表 可以看到已经非法获取R1R2的网段信息,那么现在我们R3可以向两个 ...

  8. day03——整型、字符串、for循环

    day03 整型 用于比较和运算 32位:-2 ** 31--2 ** 31-1 64位:-2 ** 63--2 ** 63-1 长整型(long) python2中有长整型.获取的是整数 pytho ...

  9. 带小伙伴手写 golang context

    前言 - context 源码 可以先了解官方 context.go 轮廓. 这里捎带保存一份当前 context 版本备份. // Copyright 2014 The Go Authors. Al ...

  10. XXE任意文件读取(当xml解析内容有输出时)

    利用XXE漏洞读取文件 参考:https://www.jianshu.com/p/4fc721398e97 首先找到登录源码如下: 由题目可以利用XXE漏洞读取文件 先登录用Burp Suite抓包: ...