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接口