我一直很困惑为啥谷歌不将Gson类中的方法改为静态方法,用起来不是更方便吗?我的前同事有好多人在结合httpclient使用时都是在发送消息的类中定义了一个属性:private static Gson gson = new Gson();调用时,就直接gson.toJson(obj);这种使用方法有没有什么潜在问题?求解答啊...
4 回答

吃鸡游戏
TA贡献1829条经验 获得超7个赞
多线程同时调用会有问题吧
以下摘自Gson User Guide:
Sometimes you need to share state across custom
serializers/deserializers (see this discussion). You can use the
following three strategies to accomplish this:
1.Store shared state in static fields
2.Declare the serializer/deserializer as inner classes of a parent type, and use the instance fields of parent type to store shared state
3.Use Java ThreadLocal
1 and 2 are not thread-safe options, but 3 is.
添加回答
举报
0/150
提交
取消