在静态方法中创建新的JAXB Marshaller实例......是ThreadSafe吗? [英] Create new JAXB Marshaller instance in static method... is it ThreadSafe?

查看:114
本文介绍了在静态方法中创建新的JAXB Marshaller实例......是ThreadSafe吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下静态方法实现了一个util类:

I have implemented a util class with the following static method:

public static String objToXml(JAXBContext jaxbContext, ClassA obj) throws Exception{
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
    StringWriter writer = new StringWriter();
    marshaller.marshal(obj, writer);
    return writer.toString();
}

我的理解是JAXB Marshaller和StringWriter都是线程安全的......但是我想双重确认我是否实现了这样的静态方法..并且有多个线程同时调用objToXml方法,是否会有任何潜在的问题?

My understanding is that JAXB Marshaller and StringWriter are both thread safe ... but I want to double confirm if I implement a static method like this.. and have multiple threads calling objToXml method concurrently, would there be any potential issue?

推荐答案

是的,如果你计划多线程使用它们,你必须每次创建一个新的 Marshaller

Yes you must create a new Marshaller each time around if you are planning to use them multi-threaded.

参见非官方的JAXB指南


JAXBContext类是线程安全的,但是Marshaller,Unmarshaller和Validator 非线程安全

这篇关于在静态方法中创建新的JAXB Marshaller实例......是ThreadSafe吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆