If I need to call a method that creates an xls
file from multiple threads - can I use XLSTransformer
as synchronized
(or even not) field of the class? And if XLSTransformer.transformXLS()
method really expensive, or it's OK to create a new instance any time I need to create xls
?
That's what I'm talking about:
private synchronized XLSTransformer transformer = new XLSTransformer();
public void createXls() {
//...
transformer.transformXLS("template.xls", beans, "result.xls");
}
Won't it break if I call createXls()
from multiple threads?
There is no single note, that XLSTransformer
is thread-safe, so you can assume it is not, or analyze jXLS source code (link). However, be informed, that it can be different in each lib version and you should check again after each update.
If it is about your example, the synchronized
keyword is illegal modifier for fields. You can use it with methods to lock on this
object:
public synchronized void createXls() {
//...
}
or with blocks when you can choose object treated as lock:
public void createXls() {
//...
synchronized( transformer ) {
//...
}
//...
}
More information in Java tutorials: