Created
August 6, 2012 23:56
-
-
Save johnnywey/3279736 to your computer and use it in GitHub Desktop.
Groovy JsonOutput failing example
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
// 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.