【发布时间】:2021-08-03 00:39:59
【问题描述】:
我真正想弄清楚的是如何思考这个问题,或者从该领域更有经验的人那里获得一些见解。
假设这是 Web 服务中的单例类,例如 graphql 数据提取器。哪个在规模上表现更好:第一个还是第二个?或者会有什么性能差异?
public class FooBarClass {
public List<Bar> getSomeStuff() {
List<Bar> bar = new ArrayList<Bar>;
final ExecutorService threadpool = Executors.newFixedThreadPool(10);
//Get the stuff in multithreaded way.
threadpool.shutdown();
return bar;
};
}
}
或
public class FooBarClass {
final ExecutorService threadpool = Executors.newFixedThreadPool(10);
public List<Bar> getSomeStuff() {
//...
//Get the stuff using the executor service.
return bar;
};
}
}
我发现使用执行器服务肯定会加快代码速度。我们正在进行类似批处理的提取,但由于某些原因我们需要进行单次提取,所以所有提取都是异步的。
【问题讨论】: