[ https://issues.apache.org/jira/browse/THRIFT-2218?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13825671#comment-13825671 ]
Alex Levenson commented on THRIFT-2218: --------------------------------------- Well, we've found that users have been (for not entirely clear reasons) expecting TSerializer / TDeserializer to be thread safe. So I'd like to do something to help people from shooting themselves in the foot. Here's a few options: 1) Make them thread safe by wrapping the shared state (the ByteArrayOutputStream etc) in ThreadLocal so that it's not shared across threads. - Seems like this would have better performance than 2 and 3 below but I can't back that up with data 2) Make them thread safe by making the methods synchronized - Seems like a bad idea for performance? 3) Make them thread safe by simply *not* sharing the state in the first place (construct a new ByteArrayOutputStream each time the method is called) - lots of object creation? 4) Add @NotThreadSafe and keep it the way it is - I'm not sure that that will help though? Anyone who looked at this class would see that it's not thread safe, so I wonder if they'll see the annotation? 5) Add static methods that are thread safe, leave the rest of the class alone - sort of an in-between 6) Add a new utility: ThreadLocalTSerializer, leave the current classes alone. - this might be the way to go? The presence of a class like this might help remind people that TSerializer isn't thread safe? > Make java TSerializer and TDeserializer thread safe > --------------------------------------------------- > > Key: THRIFT-2218 > URL: https://issues.apache.org/jira/browse/THRIFT-2218 > Project: Thrift > Issue Type: Improvement > Components: Java - Library > Reporter: Alex Levenson > Assignee: Jake Farrell > Attachments: TSerDe-thread-safe.diff > > > They currently are not thread safe, though it seems that many people assume > incorrectly that they are. > This patch puts the shared mutable state used by TSerializer / TDeserializer > in a ThreadLocal object so that it is safe to share instances of TSerializer > / TDeserializer across threads. > I've attached the patch, and you can view it in github here as well: > https://github.com/isnotinvain/thrift/compare/TSerDe-thread-safe -- This message was sent by Atlassian JIRA (v6.1#6144)