Can I access/modify workbooks/documents/slideshows in multiple threads? What are the multi-threading guarantees that Apache POI makes

前几天和财务的朋友聊天的时候,他说他们那边的Excel导出比较慢,因为导出的数据量比较大,往往几百万条,而使用单线程导出的。所以我今天项目提测以后就来写了个Demo尝试基于POI来写一个多线程的Excel导出,demo写完以后发现运行结果经常不正确,检查完程序逻辑以后,就感觉可能POI不支持多线程操作,于是上网查询了一波, 果然:

官网原文地址:Can I access/modify workbooks/documents/slideshows in multiple threads? What are the multi-threading guarantees that Apache POI makes

In short: Handling different document-objects in different threads will work. Accessing the same document in multiple threads will not work.

This means the workbook/document/slideshow objects are not checked for thread safety, but any globally held object like global caches or other data structures are guarded against multi threaded access accordingly.

There have been discussions about accessing different Workbook-sheets in different threads concurrently. While this may work to some degree, it may lead to very hard to track errors as multi-threading issues typically only manifest after long runtime when many threads are active and the system is under high load, i.e. in production use! Also it might break in future versions of Apache POI as we do not specifically test using the library this way.

本文版权归作者所有,禁止一切形式的转载,复制等操作
赞赏

微信赞赏支付宝赞赏

发表评论

电子邮件地址不会被公开。 必填项已用*标注