Java 在同一循环中调用线程启动
Java invoking thread start in same loop
我有这个循环为每个线程设置作业
我的问题是我如何在同一个循环中启动线程,并避免另一个循环
List<Thread> works= new ArrayList<Thread>();
for (final action a : actions) {
threads.add(new Thread(() -> {
jobMethod(a);
}));
}
这是我想避免的:
for (Thread t : threads) {
t.start();
}
声明一个变量?似乎相当琐碎:
Thread t = new Thread(() -> jobMethod(a));
threads.add(t);
t.start();
刚刚:
List<Thread> works= new ArrayList<Thread>();
for (final action a : actions) {
// local var
Thread tTmp = new Thread(() -> {
jobMethod(a);
});
threads.add(tTmp);
// and shoot!
tTmp.start();
}
??
;-)
我有这个循环为每个线程设置作业 我的问题是我如何在同一个循环中启动线程,并避免另一个循环
List<Thread> works= new ArrayList<Thread>();
for (final action a : actions) {
threads.add(new Thread(() -> {
jobMethod(a);
}));
}
这是我想避免的:
for (Thread t : threads) {
t.start();
}
声明一个变量?似乎相当琐碎:
Thread t = new Thread(() -> jobMethod(a));
threads.add(t);
t.start();
刚刚:
List<Thread> works= new ArrayList<Thread>();
for (final action a : actions) {
// local var
Thread tTmp = new Thread(() -> {
jobMethod(a);
});
threads.add(tTmp);
// and shoot!
tTmp.start();
}
??
;-)