Skip to content

Commit aeb719b

Browse files
author
Jano
committed
move logic from BoundedExecutor to KeySequentialBoundedExecutor
1 parent 14fa049 commit aeb719b

10 files changed

Lines changed: 314 additions & 333 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ from multiple threads without synchronization.
101101
<dependency>
102102
<groupId>com.jano7</groupId>
103103
<artifactId>executor</artifactId>
104-
<version>2.0.1</version>
104+
<version>2.0.2</version>
105105
</dependency>
106106
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@
152152
<connection>scm:git:git://github.com/jano7/executor.git</connection>
153153
<developerConnection>scm:git:git@github.com:jano7/executor.git</developerConnection>
154154
<url>https://github.com/jano7/executor</url>
155-
<tag>executor-2.0.1</tag>
155+
<tag>executor-2.0.2</tag>
156156
</scm>
157157

158158
</project>

src/main/java/com/jano7/executor/BoundedExecutor.java

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

src/main/java/com/jano7/executor/KeySequentialBoundedExecutor.java

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,74 @@ of this software and associated documentation files (the "Software"), to deal
2424
package com.jano7.executor;
2525

2626
import java.util.concurrent.Executor;
27+
import java.util.concurrent.RejectedExecutionException;
28+
import java.util.concurrent.Semaphore;
2729
import java.util.concurrent.TimeUnit;
2830

31+
import static com.jano7.executor.BoundedStrategy.BLOCK;
32+
import static com.jano7.executor.Util.checkNotNull;
33+
2934
public final class KeySequentialBoundedExecutor implements DrainableExecutor {
3035

31-
private final BoundedExecutor boundedExecutor;
36+
private final int maxTasks;
37+
38+
private final Semaphore semaphore;
39+
40+
private final KeySequentialExecutor keySequentialExecutor;
41+
42+
private final Runnable acquire;
43+
44+
private boolean drained = false;
3245

3346
public KeySequentialBoundedExecutor(int maxTasks, BoundedStrategy onTasksExceeded, Executor underlyingExecutor) {
34-
boundedExecutor = new BoundedExecutor(maxTasks, onTasksExceeded, new KeySequentialExecutor(underlyingExecutor));
47+
this.maxTasks = maxTasks;
48+
this.semaphore = new Semaphore(maxTasks);
49+
this.keySequentialExecutor = new KeySequentialExecutor(underlyingExecutor);
50+
this.acquire = onTasksExceeded == BLOCK ? this::blockOnTasksExceeded : this::rejectOnTasksExceeded;
51+
}
52+
53+
private void blockOnTasksExceeded() {
54+
semaphore.acquireUninterruptibly();
55+
}
56+
57+
private void rejectOnTasksExceeded() {
58+
if (!semaphore.tryAcquire()) {
59+
throw new RejectedExecutionException("task limit of " + maxTasks + " exceeded");
60+
}
3561
}
3662

3763
@Override
3864
public void execute(Runnable task) {
39-
boundedExecutor.execute(task);
65+
checkNotNull(task);
66+
synchronized (this) {
67+
if (drained) {
68+
throw new RejectedExecutionException("executor drained");
69+
} else {
70+
acquire.run();
71+
}
72+
}
73+
try {
74+
keySequentialExecutor.execute(new KeyRunnable<>(
75+
task,
76+
() -> {
77+
try {
78+
task.run();
79+
} finally {
80+
semaphore.release();
81+
}
82+
})
83+
);
84+
} catch (RejectedExecutionException e) {
85+
semaphore.release();
86+
throw e;
87+
}
4088
}
4189

4290
@Override
43-
public boolean drain(long timeout, TimeUnit unit) throws InterruptedException {
44-
return boundedExecutor.drain(timeout, unit);
91+
public synchronized boolean drain(long timeout, TimeUnit unit) throws InterruptedException {
92+
if (!drained && semaphore.tryAcquire(maxTasks, timeout, unit)) {
93+
drained = true;
94+
}
95+
return drained;
4596
}
4697
}

src/test/java/com/jano7/executor/BoundedExecutorTest.java

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

0 commit comments

Comments
 (0)