Skip to content

Commit ea4e950

Browse files
clmnetdpb
authored andcommitted
Add toString() for SequentialExecutor's worker runnable
RELNOTES=n/a ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=320760882
1 parent b5210ca commit ea4e950

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

android/guava-tests/test/com/google/common/util/concurrent/SequentialExecutorTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,15 @@ public void run() {
351351
}
352352

353353
public void testToString() {
354-
Executor delegate =
354+
final Runnable[] currentTask = new Runnable[1];
355+
final Executor delegate =
355356
new Executor() {
356357
@Override
357-
public void execute(Runnable task) {}
358+
public void execute(Runnable task) {
359+
currentTask[0] = task;
360+
task.run();
361+
currentTask[0] = null;
362+
}
358363

359364
@Override
360365
public String toString() {
@@ -365,5 +370,19 @@ public String toString() {
365370
Executor sequential2 = newSequentialExecutor(delegate);
366371
assertThat(sequential1.toString()).contains("theDelegate");
367372
assertThat(sequential1.toString()).isNotEqualTo(sequential2.toString());
373+
final String[] whileRunningToString = new String[1];
374+
sequential1.execute(
375+
new Runnable() {
376+
@Override
377+
public void run() {
378+
whileRunningToString[0] = "" + currentTask[0];
379+
}
380+
381+
@Override
382+
public String toString() {
383+
return "my runnable's toString";
384+
}
385+
});
386+
assertThat(whileRunningToString[0]).contains("my runnable's toString");
368387
}
369388
}

android/guava/src/com/google/common/util/concurrent/SequentialExecutor.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public void execute(final Runnable task) {
120120
public void run() {
121121
task.run();
122122
}
123+
124+
@Override
125+
public String toString() {
126+
return task.toString();
127+
}
123128
};
124129
queue.add(submittedTask);
125130
workerRunningState = QUEUING;
@@ -165,6 +170,8 @@ public void run() {
165170

166171
/** Worker that runs tasks from {@link #queue} until it is empty. */
167172
private final class QueueWorker implements Runnable {
173+
Runnable task;
174+
168175
@Override
169176
public void run() {
170177
try {
@@ -196,7 +203,6 @@ private void workOnQueue() {
196203
boolean hasSetRunning = false;
197204
try {
198205
while (true) {
199-
Runnable task;
200206
synchronized (queue) {
201207
// Choose whether this thread will run or not after acquiring the lock on the first
202208
// iteration
@@ -227,6 +233,8 @@ private void workOnQueue() {
227233
task.run();
228234
} catch (RuntimeException e) {
229235
log.log(Level.SEVERE, "Exception while executing runnable " + task, e);
236+
} finally {
237+
task = null;
230238
}
231239
}
232240
} finally {
@@ -238,6 +246,16 @@ private void workOnQueue() {
238246
}
239247
}
240248
}
249+
250+
@SuppressWarnings("GuardedBy")
251+
@Override
252+
public String toString() {
253+
Runnable currentlyRunning = task;
254+
if (currentlyRunning != null) {
255+
return "SequentialExecutorWorker{running=" + currentlyRunning + "}";
256+
}
257+
return "SequentialExecutorWorker{state=" + workerRunningState + "}";
258+
}
241259
}
242260

243261
@Override

guava-tests/test/com/google/common/util/concurrent/SequentialExecutorTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,15 @@ public void run() {
351351
}
352352

353353
public void testToString() {
354-
Executor delegate =
354+
final Runnable[] currentTask = new Runnable[1];
355+
final Executor delegate =
355356
new Executor() {
356357
@Override
357-
public void execute(Runnable task) {}
358+
public void execute(Runnable task) {
359+
currentTask[0] = task;
360+
task.run();
361+
currentTask[0] = null;
362+
}
358363

359364
@Override
360365
public String toString() {
@@ -365,5 +370,19 @@ public String toString() {
365370
Executor sequential2 = newSequentialExecutor(delegate);
366371
assertThat(sequential1.toString()).contains("theDelegate");
367372
assertThat(sequential1.toString()).isNotEqualTo(sequential2.toString());
373+
final String[] whileRunningToString = new String[1];
374+
sequential1.execute(
375+
new Runnable() {
376+
@Override
377+
public void run() {
378+
whileRunningToString[0] = "" + currentTask[0];
379+
}
380+
381+
@Override
382+
public String toString() {
383+
return "my runnable's toString";
384+
}
385+
});
386+
assertThat(whileRunningToString[0]).contains("my runnable's toString");
368387
}
369388
}

guava/src/com/google/common/util/concurrent/SequentialExecutor.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ public void execute(final Runnable task) {
120120
public void run() {
121121
task.run();
122122
}
123+
124+
@Override
125+
public String toString() {
126+
return task.toString();
127+
}
123128
};
124129
queue.add(submittedTask);
125130
workerRunningState = QUEUING;
@@ -165,6 +170,8 @@ public void run() {
165170

166171
/** Worker that runs tasks from {@link #queue} until it is empty. */
167172
private final class QueueWorker implements Runnable {
173+
Runnable task;
174+
168175
@Override
169176
public void run() {
170177
try {
@@ -196,7 +203,6 @@ private void workOnQueue() {
196203
boolean hasSetRunning = false;
197204
try {
198205
while (true) {
199-
Runnable task;
200206
synchronized (queue) {
201207
// Choose whether this thread will run or not after acquiring the lock on the first
202208
// iteration
@@ -227,6 +233,8 @@ private void workOnQueue() {
227233
task.run();
228234
} catch (RuntimeException e) {
229235
log.log(Level.SEVERE, "Exception while executing runnable " + task, e);
236+
} finally {
237+
task = null;
230238
}
231239
}
232240
} finally {
@@ -238,6 +246,16 @@ private void workOnQueue() {
238246
}
239247
}
240248
}
249+
250+
@SuppressWarnings("GuardedBy")
251+
@Override
252+
public String toString() {
253+
Runnable currentlyRunning = task;
254+
if (currentlyRunning != null) {
255+
return "SequentialExecutorWorker{running=" + currentlyRunning + "}";
256+
}
257+
return "SequentialExecutorWorker{state=" + workerRunningState + "}";
258+
}
241259
}
242260

243261
@Override

0 commit comments

Comments
 (0)