Last active
August 29, 2015 13:56
-
-
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?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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