zoukankan      html  css  js  c++  java
  • 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 -
     %(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快速入门 ---- 系列文章
    批处理中的时间计算详解
    使用electron+vue开发一个跨平台todolist(便签)桌面应用
    文科妹子都会用 GitHub,你这个工科生还等什么
    如约而至,.NET 5.0 正式发布
    如何进行正确的沟通?
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/12305137.html
Copyright © 2011-2022 走看看