Skip to content

Commit

Permalink
[guava concurrent] Removed the executeFirst() method from SequentialE…
Browse files Browse the repository at this point in the history
…xecutor, and its test case. Switched the internal representation of the task queue back to Queue from Deque.

The method was never public, and is no longer used in the depot.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=170244464
  • Loading branch information
yorickhenning authored and cpovirk committed Sep 27, 2017
1 parent 157f1d2 commit 5075255
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 90 deletions.
Expand Up @@ -179,30 +179,6 @@ public void run() {
assertEquals(ImmutableList.of(0, 1, 2), callOrder); assertEquals(ImmutableList.of(0, 1, 2), callOrder);
} }


public void testPrependContinuation() {
final List<Integer> callOrder = Lists.newArrayList();

class FakeOp implements Runnable {
final int op;

FakeOp(int op) {
this.op = op;
}

@Override
public void run() {
callOrder.add(op);
}
}

e.execute(new FakeOp(1));
e.execute(new FakeOp(2));
e.executeFirst(new FakeOp(0));
fakePool.runAll();

assertEquals(ImmutableList.of(0, 1, 2), callOrder);
}

public void testRuntimeException_doesNotStopExecution() { public void testRuntimeException_doesNotStopExecution() {


final AtomicInteger numCalls = new AtomicInteger(); final AtomicInteger numCalls = new AtomicInteger();
Expand Down
Expand Up @@ -19,7 +19,7 @@
import com.google.errorprone.annotations.DoNotMock; import com.google.errorprone.annotations.DoNotMock;
import com.google.j2objc.annotations.WeakOuter; import com.google.j2objc.annotations.WeakOuter;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Deque; import java.util.Queue;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
Expand Down Expand Up @@ -53,7 +53,7 @@ class SequentialExecutor implements Executor {
private final Executor executor; private final Executor executor;


@GuardedBy("queue") @GuardedBy("queue")
private final Deque<Runnable> queue = new ArrayDeque<>(); private final Queue<Runnable> queue = new ArrayDeque<>();


@GuardedBy("queue") @GuardedBy("queue")
private boolean isWorkerRunning = false; private boolean isWorkerRunning = false;
Expand All @@ -78,22 +78,7 @@ class SequentialExecutor implements Executor {
@Override @Override
public void execute(Runnable task) { public void execute(Runnable task) {
synchronized (queue) { synchronized (queue) {
queue.addLast(task); queue.add(task);
if (isWorkerRunning || suspensions > 0) {
return;
}
isWorkerRunning = true;
}
startQueueWorker();
}

/**
* Prepends a task to the front of the queue and makes sure a worker thread is running, unless the
* queue has been suspended.
*/
public void executeFirst(Runnable task) {
synchronized (queue) {
queue.addFirst(task);
if (isWorkerRunning || suspensions > 0) { if (isWorkerRunning || suspensions > 0) {
return; return;
} }
Expand Down Expand Up @@ -122,8 +107,8 @@ public void suspend() {
* if the suspension counter is zero. * if the suspension counter is zero.
* *
* <p>If this method throws, e.g. a {@code RejectedExecutionException} from the delegate executor, * <p>If this method throws, e.g. a {@code RejectedExecutionException} from the delegate executor,
* execution of tasks will stop until a call to this method or to {@link #execute(Runnable)} or * execution of tasks will stop until a call to this method or to {@link #execute(Runnable)} is
* {@link #executeFirst(Runnable)} is made. * made.
* *
* @throws java.lang.IllegalStateException if this executor is not suspended. * @throws java.lang.IllegalStateException if this executor is not suspended.
*/ */
Expand Down Expand Up @@ -191,7 +176,7 @@ private void workOnQueue() {
synchronized (queue) { synchronized (queue) {
// TODO(user): How should we handle interrupts and shutdowns? // TODO(user): How should we handle interrupts and shutdowns?
if (suspensions == 0) { if (suspensions == 0) {
task = queue.pollFirst(); task = queue.poll();
} }
if (task == null) { if (task == null) {
isWorkerRunning = false; isWorkerRunning = false;
Expand Down
Expand Up @@ -179,30 +179,6 @@ public void run() {
assertEquals(ImmutableList.of(0, 1, 2), callOrder); assertEquals(ImmutableList.of(0, 1, 2), callOrder);
} }


public void testPrependContinuation() {
final List<Integer> callOrder = Lists.newArrayList();

class FakeOp implements Runnable {
final int op;

FakeOp(int op) {
this.op = op;
}

@Override
public void run() {
callOrder.add(op);
}
}

e.execute(new FakeOp(1));
e.execute(new FakeOp(2));
e.executeFirst(new FakeOp(0));
fakePool.runAll();

assertEquals(ImmutableList.of(0, 1, 2), callOrder);
}

public void testRuntimeException_doesNotStopExecution() { public void testRuntimeException_doesNotStopExecution() {


final AtomicInteger numCalls = new AtomicInteger(); final AtomicInteger numCalls = new AtomicInteger();
Expand Down
Expand Up @@ -19,7 +19,7 @@
import com.google.errorprone.annotations.DoNotMock; import com.google.errorprone.annotations.DoNotMock;
import com.google.j2objc.annotations.WeakOuter; import com.google.j2objc.annotations.WeakOuter;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Deque; import java.util.Queue;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
Expand Down Expand Up @@ -53,7 +53,7 @@ class SequentialExecutor implements Executor {
private final Executor executor; private final Executor executor;


@GuardedBy("queue") @GuardedBy("queue")
private final Deque<Runnable> queue = new ArrayDeque<>(); private final Queue<Runnable> queue = new ArrayDeque<>();


@GuardedBy("queue") @GuardedBy("queue")
private boolean isWorkerRunning = false; private boolean isWorkerRunning = false;
Expand All @@ -78,22 +78,7 @@ class SequentialExecutor implements Executor {
@Override @Override
public void execute(Runnable task) { public void execute(Runnable task) {
synchronized (queue) { synchronized (queue) {
queue.addLast(task); queue.add(task);
if (isWorkerRunning || suspensions > 0) {
return;
}
isWorkerRunning = true;
}
startQueueWorker();
}

/**
* Prepends a task to the front of the queue and makes sure a worker thread is running, unless the
* queue has been suspended.
*/
public void executeFirst(Runnable task) {
synchronized (queue) {
queue.addFirst(task);
if (isWorkerRunning || suspensions > 0) { if (isWorkerRunning || suspensions > 0) {
return; return;
} }
Expand Down Expand Up @@ -122,8 +107,8 @@ public void suspend() {
* if the suspension counter is zero. * if the suspension counter is zero.
* *
* <p>If this method throws, e.g. a {@code RejectedExecutionException} from the delegate executor, * <p>If this method throws, e.g. a {@code RejectedExecutionException} from the delegate executor,
* execution of tasks will stop until a call to this method or to {@link #execute(Runnable)} or * execution of tasks will stop until a call to this method or to {@link #execute(Runnable)} is
* {@link #executeFirst(Runnable)} is made. * made.
* *
* @throws java.lang.IllegalStateException if this executor is not suspended. * @throws java.lang.IllegalStateException if this executor is not suspended.
*/ */
Expand Down Expand Up @@ -191,7 +176,7 @@ private void workOnQueue() {
synchronized (queue) { synchronized (queue) {
// TODO(user): How should we handle interrupts and shutdowns? // TODO(user): How should we handle interrupts and shutdowns?
if (suspensions == 0) { if (suspensions == 0) {
task = queue.pollFirst(); task = queue.poll();
} }
if (task == null) { if (task == null) {
isWorkerRunning = false; isWorkerRunning = false;
Expand Down

0 comments on commit 5075255

Please sign in to comment.