Skip to content

Instantly share code, notes, and snippets.

@bjpeterdelacruz
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjpeterdelacruz/9046356 to your computer and use it in GitHub Desktop.
Save bjpeterdelacruz/9046356 to your computer and use it in GitHub Desktop.
Bug Patrol #1. There's a bug in this code snippet. Can you find it?
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final ExecutorService executor = Executors.newFixedThreadPool(1);
private final Set<String> serialsSet = new HashSet<String>();
private static final int NUM_SECONDS = 3;
// ...
private void scheduleTask(final String serial) {
// Assume this method is called from a thread in executor.
if (!this.serialsSet.contains(serial)) {
this.serialsSet.add(serial);
Runnable task = new Runnable() {
/** {@inheritDoc} */
@Override
public void run() {
executor.execute(new Runnable() {
/** {@inheritDoc} */
@Override
public void run() {
processSerial(serial);
serialsSet.remove(serial);
}
});
}
};
this.scheduler.schedule(task, NUM_SECONDS,
TimeUnit.SECONDS);
}
}
private void processSerial(String serial) throws Exception {
// Do something here that may throw an exception.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment