1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.lifecycle.internal.concurrent;
20
21 import java.util.concurrent.Executor;
22 import java.util.concurrent.ExecutorService;
23 import java.util.concurrent.Phaser;
24
25 public class PhasingExecutor implements Executor, AutoCloseable {
26 private final ExecutorService executor;
27 private final Phaser phaser = new Phaser();
28
29 public PhasingExecutor(ExecutorService executor) {
30 this.executor = executor;
31 this.phaser.register();
32 }
33
34 @Override
35 public void execute(Runnable command) {
36 phaser.register();
37 executor.submit(() -> {
38 try {
39 command.run();
40 } finally {
41 phaser.arriveAndDeregister();
42 }
43 });
44 }
45
46 public void await() {
47 phaser.arriveAndAwaitAdvance();
48 }
49
50 @Override
51 public void close() {
52 executor.shutdownNow();
53 }
54 }