0%

java | submit invokeAll invokeAny

提交任务。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 执行任务
void execute(Runnable command)

//提交任务 task,用返回值 Future 获得任务执行结果
<T> Future<T> submit(Callable<T> task);

//提交 tasks 所有任务
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException;

//提交 tasks 中所有任务,带有超时时间
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout,TimeUnint unit) throws InterruptedException;

//提交 tasks 所有任务,哪个任务先成功执行完毕,返回次任务执行结果,其他任务取消
<T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException,ExecutionException;

//提交 tasks 中所有任务,哪个任务先成功执行完毕,返回次任务执行结果,其他任务取消,带超时时间
<T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout,TimeUnint unit) throws InterruptedException,ExecutionException,TineoutException;

submit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.redisc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j(topic = "c.Test")
public class Run {

public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService pool = Executors.newFixedThreadPool(2);
Future<String> future = pool.submit(new Callable<String>() {
@Override
public String call() throws Exception {
log.debug("running");
Thread.sleep(1000);
return "OK";
}
});

log.debug("{}", future.get());
}
}

输出

1
2
14:17:35.533 [pool-1-thread-1] DEBUG c.Test - running
14:17:36.538 [main] DEBUG c.Test - OK

invokeAll

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.redisc;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

@Slf4j(topic = "c.Test")
public class Run {

public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService pool = Executors.newFixedThreadPool(2);

List<Future<String>> futureList = pool.invokeAll(Arrays.asList(
() -> {
log.debug("begin");
Thread.sleep(1000);
return "1";
},
() -> {
log.debug("begin");
Thread.sleep(1000);
return "2";
},
() -> {
log.debug("begin");
Thread.sleep(1000);
return "3";
}
));
futureList.forEach(f -> {
try {
log.debug("{}", f.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
});
}
}

输出

1
2
3
4
5
6
22:01:01.831 [pool-1-thread-1] DEBUG c.Test - begin
22:01:01.831 [pool-1-thread-2] DEBUG c.Test - begin
22:01:02.838 [pool-1-thread-1] DEBUG c.Test - begin
22:01:03.843 [main] DEBUG c.Test - 1
22:01:03.845 [main] DEBUG c.Test - 2
22:01:03.845 [main] DEBUG c.Test - 3

invokeAny

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.redisc;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.*;

@Slf4j(topic = "c.Test")
public class Run {

public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService pool = Executors.newFixedThreadPool(2);

String s = pool.invokeAny(Arrays.asList(
() -> {
log.debug("begin");
Thread.sleep(1000);
return "1";
},
() -> {
log.debug("begin");
Thread.sleep(1000);
return "2";
},
() -> {
log.debug("begin");
Thread.sleep(1000);
return "3";
}
));
log.debug("{}", s);
}
}

输出

1
2
3
4
22:08:37.336 [pool-1-thread-1] DEBUG c.Test - begin
22:08:37.336 [pool-1-thread-2] DEBUG c.Test - begin
22:08:38.339 [pool-1-thread-2] DEBUG c.Test - begin
22:08:38.339 [main] DEBUG c.Test - 1
请我喝杯咖啡吧~