多线程实现
多线程-不需要返回结果实现多线程-需要返回结果实现
多线程-不需要返回结果实现
public void Task(){
ExecutorService executorService
= Executors
.newFixedThreadPool(3);
for (int i
=0;i
<600;i
++){
executorService
.submit(new FindAndSend(i
));
}
}
class FindAndSend implements Runnable{
private int i
;
public FindAndSend(int i
) {
this.i
= i
;
}
@Override
public void run() {
logger
.error("多线程"+i
);
}
}
多线程-需要返回结果实现
public void sum() throws ExecutionException
, InterruptedException
{
ExecutorService executorService
= Executors
.newFixedThreadPool(4);
List
<Future
<Integer>> futureList
= new ArrayList<>();
for (int i
=0;i
<600;i
++){
Future
<Integer> futureSum
= executorService
.submit(new SumCall(Integer
.valueOf(i
)));
futureList
.add(futureSum
);
}
for (Future
<Integer> future
: futureList
){
System
.out
.println(future
.get());
}
}
class SumCall implements Callable<Integer> {
private int plus
;
public SumCall(int plus
) {
this.plus
= plus
;
}
@Override
public Integer
call() throws Exception
{
return 1+plus
;
}
}