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 立即关闭线程池
}
}


浙公网安备 33010602011771号