Skip to content

Instantly share code, notes, and snippets.

@johnnywey
Created August 6, 2012 23:56
Show Gist options
  • Save johnnywey/3279736 to your computer and use it in GitHub Desktop.
Save johnnywey/3279736 to your computer and use it in GitHub Desktop.
Groovy JsonOutput failing example
// Groovy's JsonOutput is using a static SimpleDateFormat. This is not threadsafe.
import java.text.SimpleDateFormat
import java.util.concurrent.LinkedBlockingQueue
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.TimeUnit
ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 4, 500, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(1000))
1000.times {executor.execute(new KillJsonOutput())}
while (true) {
sleep(5000)
}
class KillJsonOutput implements Runnable {
private SimpleDateFormat formatterLocal = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US)
@Override
void run() {
formatterLocal.timeZone = TimeZone.getTimeZone('GMT')
def date = new Date()
sleep(new Random().nextInt(1000))
date.setHours(new Random().nextInt(24))
assert "\"${formatterLocal.format(date)}\"" == groovy.json.JsonOutput.toJson(date)
}
}
@johnnywey
Copy link
Author

Krufty example of the 'ol static SimpleDateFormat not being threadsafe problem. Would love to see Oracle actually make this threadsafe some time in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment