我有一个列表说有登录名,基于列表内容,我正在处理其他方法,例如:
while ( it.hasNext() )
{
method1();
}
由于这些任务是独立的,我不想等待序列,所以我想并行运行它们。
我做了以下操作:
ExecutorService service = Executors.newFixedThreadPool(5);
final List<Future<?>> futures = new ArrayList<>();
while ( it.hasNext() )
{
Future<?> future = service.submit(() -> {
method1(it.next());
});
futures.add(future);
}
但问题是,它为一个登录名执行 method1 5 次...但我想要 5 个线程独立执行 method1。我哪里做错了?
爱!!
阿晨1998
杨__羊羊
LEATH
相关分类