Improved: [FB] Package org.apache.ofbiz.base.concurrent
Fixes some diamond operators
Implementes equals method to fix potential problems
Implementes hashCode method because I implemented equals method
Implementes timeDiff method to keep the code DRY
Thanks: Dennis Balkir
@@ -41,7 +41,7 @@ import org.apache.ofbiz.base.util.Debug;
@SourceMonitored
public final class ExecutionPool {
public static final String module = ExecutionPool.class.getName();
- public static final ExecutorService GLOBAL_BATCH = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ExecutionPoolThreadFactory(null, "OFBiz-batch"));
+ public static final ExecutorService GLOBAL_BATCH = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 5, TimeUnit.SECONDS, new SynchronousQueue<>(), new ExecutionPoolThreadFactory(null, "OFBiz-batch"));
public static final ForkJoinPool GLOBAL_FORK_JOIN = new ForkJoinPool();
private static final ExecutorService pulseExecutionPool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), new ExecutionPoolThreadFactory(null, "OFBiz-ExecutionPoolPulseWorker"));
@@ -77,7 +77,7 @@ public final class ExecutionPool {
}
public static <F> List<F> getAllFutures(Collection<Future<F>> futureList) {
- List<F> result = new LinkedList<F>();
+ List<F> result = new LinkedList<>();
for (Future<F> future: futureList) {
try {
result.add(future.get());
@@ -105,7 +105,7 @@ public final class ExecutionPool {
}
}
- private static final DelayQueue<Pulse> delayQueue = new DelayQueue<Pulse>();
+ private static final DelayQueue<Pulse> delayQueue = new DelayQueue<>();
public static class ExecutionPoolPulseWorker implements Runnable {
public void run() {
@@ -145,11 +145,33 @@ public final class ExecutionPool {
}
public final int compareTo(Delayed other) {
- long r = (expireTimeNanos - ((Pulse) other).expireTimeNanos);
- if (r < 0) return -1;
- if (r > 0) return 1;
+ if (this.equals(other)) {
return 0;
}
+ long r = timeDiff((Pulse) other);
+ if (r < 0) return -1;
+ return 1;
+ }
+
+ public final boolean equals(Object other) {
+ if(other instanceof Pulse) {
+ return timeDiff((Pulse) other) == 0;
+ }
+ return false;
+ }
+
+ public int hashCode() {
+ return super.hashCode();
+ }
+
+ /**
+ * Calculates the difference between this.expireTimeNanos and other.expireTimeNanos
+ * @param other used to calculate the difference
+ * @return the time difference of the two instance's expireTimeNanos
+ */
+ public long timeDiff(Pulse other) {
+ return expireTimeNanos - other.expireTimeNanos;
+ }
}
}
