Skip to content

Commit a6cd94a

Browse files
committed
+ replaced Terminable with AutoCloseable
1 parent 43bf581 commit a6cd94a

File tree

8 files changed

+68
-186
lines changed

8 files changed

+68
-186
lines changed

README.md

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ etc...
6767
#### API
6868

6969
```java
70-
public interface SequentialExecutorServiceFactory extends Terminable {
70+
public interface SequentialExecutorServiceFactory extends AutoCloseable {
7171
/**
7272
* @param sequenceKey
7373
* an {@link Object} instance whose hash code is used to summon the corresponding executor.
@@ -78,40 +78,6 @@ public interface SequentialExecutorServiceFactory extends Terminable {
7878
}
7979
```
8080

81-
where ```Terminable``` is defined as
82-
83-
```java
84-
public interface Terminable {
85-
/**
86-
* Initiates an orderly shutdown of all managed thread resources. Previously submitted tasks are executed, but no
87-
* new tasks will be accepted. Invocation has no additional effect if already shut down.
88-
* <p>
89-
* This method does not wait for the previously submitted tasks to complete execution. Use an external awaiting
90-
* mechanism to do that, with the help of {@link #isTerminated()}.
91-
*/
92-
void shutdown();
93-
94-
/**
95-
* Non-blocking
96-
*
97-
* @return true if all tasks of all managed executors have completed following shut down. Note that isTerminated is
98-
* never true unless shutdown was called first.
99-
*/
100-
boolean isTerminated();
101-
102-
/**
103-
* Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the
104-
* tasks that were awaiting execution.
105-
* <p>
106-
* This method does not wait for the previously submitted tasks to complete execution. Use an external awaiting
107-
* mechanism to do that, with the help of {@link #isTerminated()}.
108-
*
109-
* @return Tasks submitted but never started executing
110-
*/
111-
List<Runnable> shutdownNow();
112-
}
113-
```
114-
11581
This API style loosely takes the form of "thread affinity". Sequence keys are used to summon executors of JDK
11682
type [ExecutorService](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html). The same
11783
sequence key always gets back the same sequential executor. All tasks of that sequence key can then be "affined" to and
@@ -189,7 +155,7 @@ public class MessageConsumer {
189155
#### API
190156

191157
```java
192-
public interface SequentialExecutor extends Terminable {
158+
public interface SequentialExecutor extends AutoCloseable {
193159
/**
194160
* @param command
195161
* the Runnable task to run sequentially with others under the same sequence key

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<modelVersion>4.0.0</modelVersion>
2828
<groupId>io.github.q3769</groupId>
2929
<artifactId>conseq4j</artifactId>
30-
<version>20230923.0.20230924</version>
30+
<version>20230924.0.0</version>
3131
<packaging>jar</packaging>
3232
<name>conseq4j</name>
3333
<description>A Java concurrent API to sequence related tasks while concurring unrelated ones</description>

src/main/java/conseq4j/Terminable.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

src/main/java/conseq4j/execute/ConseqExecutor.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
import javax.annotation.Nonnull;
3131
import javax.annotation.concurrent.ThreadSafe;
32-
import java.util.List;
3332
import java.util.Map;
3433
import java.util.concurrent.*;
3534

@@ -40,7 +39,7 @@
4039
*/
4140
@ThreadSafe
4241
@ToString
43-
public final class ConseqExecutor implements SequentialExecutor {
42+
public final class ConseqExecutor implements SequentialExecutor, AutoCloseable {
4443

4544
private final Map<Object, CompletableFuture<?>> activeSequentialTasks = new ConcurrentHashMap<>();
4645
private final ExecutorService adminService = Executors.newSingleThreadExecutor();
@@ -150,23 +149,9 @@ public <T> CompletableFuture<T> submit(@NonNull Callable<T> task, @NonNull Objec
150149
}
151150

152151
@Override
153-
public void shutdown() {
154-
new Thread(() -> {
155-
workerExecutorService.close();
156-
adminService.shutdown();
157-
}).start();
158-
}
159-
160-
@Override
161-
public boolean isTerminated() {
162-
return this.workerExecutorService.isTerminated() && this.adminService.isTerminated();
163-
}
164-
165-
@Override
166-
public @Nonnull List<Runnable> shutdownNow() {
167-
List<Runnable> neverStartedTasks = workerExecutorService.shutdownNow();
168-
adminService.shutdownNow();
169-
return neverStartedTasks;
152+
public void close() {
153+
workerExecutorService.close();
154+
adminService.close();
170155
}
171156

172157
int estimateActiveExecutorCount() {

src/main/java/conseq4j/execute/SequentialExecutor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424

2525
package conseq4j.execute;
2626

27-
import conseq4j.Terminable;
28-
2927
import java.util.concurrent.Callable;
3028
import java.util.concurrent.Future;
3129

@@ -44,7 +42,7 @@
4442
*
4543
* @author Qingtian Wang
4644
*/
47-
public interface SequentialExecutor extends Terminable {
45+
public interface SequentialExecutor {
4846
/**
4947
* @param command
5048
* the Runnable task to run sequentially with others under the same sequence key

src/main/java/conseq4j/summon/ConseqServiceFactory.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@
2929

3030
import javax.annotation.Nonnull;
3131
import javax.annotation.concurrent.ThreadSafe;
32-
import java.util.Collection;
3332
import java.util.List;
3433
import java.util.Objects;
3534
import java.util.concurrent.ConcurrentHashMap;
3635
import java.util.concurrent.ConcurrentMap;
3736
import java.util.concurrent.ExecutorService;
3837
import java.util.concurrent.Executors;
39-
import java.util.stream.Collectors;
4038

4139
import static java.lang.Math.floorMod;
4240

@@ -49,7 +47,7 @@
4947

5048
@ThreadSafe
5149
@ToString
52-
public final class ConseqServiceFactory implements SequentialExecutorServiceFactory {
50+
public final class ConseqServiceFactory implements SequentialExecutorServiceFactory, AutoCloseable {
5351
private final int concurrency;
5452
private final ConcurrentMap<Object, ShutdownDisabledExecutorService> sequentialExecutors;
5553

@@ -92,22 +90,8 @@ public ExecutorService getExecutorService(@NonNull Object sequenceKey) {
9290
}
9391

9492
@Override
95-
public void shutdown() {
96-
this.sequentialExecutors.values().parallelStream().forEach(ShutdownDisabledExecutorService::shutdownDelegate);
97-
}
98-
99-
@Override
100-
public boolean isTerminated() {
101-
return this.sequentialExecutors.values().stream().allMatch(ExecutorService::isTerminated);
102-
}
103-
104-
@Override
105-
public List<Runnable> shutdownNow() {
106-
return sequentialExecutors.values()
107-
.parallelStream()
108-
.map(ShutdownDisabledExecutorService::shutdownDelegateNow)
109-
.flatMap(Collection::stream)
110-
.collect(Collectors.toList());
93+
public void close() {
94+
sequentialExecutors.values().forEach(ExecutorService::close);
11195
}
11296

11397
private int bucketOf(Object sequenceKey) {

src/main/java/conseq4j/summon/SequentialExecutorServiceFactory.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
*/
2424
package conseq4j.summon;
2525

26-
import conseq4j.Terminable;
27-
2826
import java.util.concurrent.ExecutorService;
2927

3028
/**
@@ -36,7 +34,7 @@
3634
*
3735
* @author Qingtian Wang
3836
*/
39-
public interface SequentialExecutorServiceFactory extends Terminable {
37+
public interface SequentialExecutorServiceFactory {
4038
/**
4139
* @param sequenceKey
4240
* an {@link Object} instance whose hash code is used to summon the corresponding executor.

0 commit comments

Comments
 (0)