Skip to content

Commit

Permalink
Reduce AutoBoxing-induced GC churn by using AtomicLongMap.
Browse files Browse the repository at this point in the history
--
MOS_MIGRATED_REVID=107806099
  • Loading branch information
ericfelly authored and damienmg committed Nov 16, 2015
1 parent e258d1d commit 5023046
Showing 1 changed file with 5 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.AtomicLongMap;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionException;
Expand Down Expand Up @@ -107,7 +106,7 @@ public ThreadPoolExecutor apply(ExecutorParams p) {
private boolean jobsMustBeStopped = false;

/** Map from thread to number of jobs executing in the thread. Used for interrupt handling. */
private final Map<Thread, Long> jobs = Maps.newConcurrentMap();
private final AtomicLongMap<Thread> jobs = AtomicLongMap.create();

/** The {@link ExecutorService}. If !{@code concurrent}, this may be {@code null}. */
@Nullable private final ExecutorService executorService;
Expand Down Expand Up @@ -516,20 +515,12 @@ public void run() {
}

private void addJob(Thread thread) {
// Note: this looks like a check-then-act race but it isn't, because each
// key implies thread-locality.
long count = jobs.containsKey(thread) ? jobs.get(thread) + 1 : 1;
jobs.put(thread, count);
jobs.incrementAndGet(thread);
}

private void removeJob(Thread thread) {
Long boxedCount = Preconditions.checkNotNull(jobs.get(thread),
"Can't retrieve job after successfully adding it");
long count = boxedCount - 1;
if (count == 0) {
if (jobs.decrementAndGet(thread) == 0) {
jobs.remove(thread);
} else {
jobs.put(thread, count);
}
}

Expand Down Expand Up @@ -661,7 +652,7 @@ private void reallyAwaitTermination(boolean interruptWorkers) {

private void interruptInFlightTasks() {
Thread thisThread = Thread.currentThread();
for (Thread thread : jobs.keySet()) {
for (Thread thread : jobs.asMap().keySet()) {
if (thisThread != thread) {
thread.interrupt();
}
Expand Down

0 comments on commit 5023046

Please sign in to comment.