JDK8线程池/线程执行高负载方法

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

  public void xxMethod() {   
// 创建线程池   ExecutorService threadPool = Executors.newFixedThreadPool(list.size());   // 开启线程/循环执行   for (Object object: list) {   String xxx = object.getXxx();   String yyy = object.getYyy();   threadPool.submit(new XxClass(xxx , yyy));   }

  }
/** * 线程内部类 */ class XxClass implements Runnable { private String xxx; private String yyy; public XxClass (String xxx, String yyy) { this.xxx= xxx; this.yyy= yyy; } @Override public void run() { xxMethod(xxx, yyy); }

   }

线程池大小根据业务需求调整

如需返回值可实现Callable接口

posted on 2018-08-17 11:43  HuaChenYing  阅读(2010)  评论(0编辑  收藏  举报

导航