美文网首页JAVA
ExecutorService 并发线程的使用

ExecutorService 并发线程的使用

作者: angelali_90b4 | 来源:发表于2023-02-02 16:15 被阅读0次
            ExecutorService touchWorker = Executors.newFixedThreadPool(4, new ThreadFactoryBuilder().setNameFormat("touch-send-worker-%d").build());
            int size = orgList.size();
            if(size > 100)
            {
                int batch = size % 100 == 0 ? size / 100 : size / 100 + 1;
                for(int j = 0; j < batch; j++)
                {
                    int end = (j + 1) * 100;
                    if(end > size)
                    {
                        end = size;
                    }
                    List <OrganizeStructure> subList = orgList.subList(j * 100, end);
                    touchWorker.execute( () -> this.sleepOrg(subList));
                }
                touchWorker.shutdown();
                while(true)
                {
                    if(touchWorker.isTerminated())
                    {
                      //全部线程执行结束
                        break;
                    }
                }
            }
            else
            {
                this.sleepOrg(orgList);
            }
    
    

    相关文章

      网友评论

        本文标题:ExecutorService 并发线程的使用

        本文链接:https://www.haomeiwen.com/subject/mztghdtx.html