Free Support Forum - aspose.com

Update a workbook in multiple threads concurrently using Aspose.Cells for Java

Dear Aspose Support Team,


I have the following scenario, can you please suggest how to implement this in ASPOSE.

We have a source workbook with some data and has around 10 worksheets in it.

In our java program, we have created 10 threads and each thread will work on the same work book instance as shown below. Here each thread will work on only one sheet of the workbook. But our understanding is that even thought it is sheet as it is part of the workbook, meaning we are sharing the workbook with these 10 threads.

//load workbook from class path

Workbook srcWrkbook;

Workbook wrkbook = new Workbook();

wrkbook.copy(srcWrkbook);

//passin the wrkbook to each thread and get the same instance updated.

ThreadCalalbe1 t1 = new ThredCallable1();

ThreadCalalbe2 t2 = new ThredCallable2();

ThreadCalalbe3 t3 = new ThredCallable3();

ThreadCalalbe4 t4 = new ThredCallable4();

---- till 10 threads.
and add these 10 threads to a set.
Set<Callable> callableSet;

and then we are invoking all the thread as follows.

execService.invokeAll(callableSet); // all thread started parallely


Question here is;
since the workbook is shared among the threads, we will be experiencing the concurrent exception because two threads will try to update the same work book at the same time. we need to know how to make this workbook sharable and will not cause Concurrent Exception when used in java multithreaded environment.






Hi,


We have evaluated your scenario/ case a bit. We think and I am afraid it is impossible to update a workbook in multiple threads concurrently. Even if you do not get the concurrent exception, yet you will get some unexpected results that could be generated when manipulating the workbook in multiple-threaded environment.

FYI, if you need to read data only (using Cell.getValue()) in a shared workbook scenario, you may do it. Aspose.Cells provides an attribute for this purpose: i.e., Cells.setMultiThreadReading(), so you may use it if you got to read data (via Cell.getValue()) only in multiple threads.

PS. for your information, there is a global shared string table in an Excel workbook. So if you try to change a single workbook data concurrently in multi-threads, the result might be unstable. There is also a global section in an Excel file which contains global information of drawing objects and other information etc. So, if you want to change the objects settings concurrently in multi-threads, the result might be unstable too. It is to be noted here this is a limitation of MS Excel formats and nothing to do with Aspose.Cells APIs.

Thanks for your understanding!

Thanks Amjad Sahi for your reply.


In Normal Excel Workbook, in order for different persons to work on the same work(let say different worksheets by each user and they will not update the same sheet the other user is using) by sharing it.

This option is available in the Microsoft Excel workbook and supported by multiple user (same as threads) updates.

1) Do Aspose.cells has the sharable feature(Share workbook feature as in MS Excel)
2) Do you think that the data will not be stable even if the source template that is being shared with multiple threads is shared(Share workbook feature as in MS Excel)?

Thanks
Esha


Hi Esha,

Thanks for your posting and using Aspose.Cells.

We are afraid, you cannot use the single workbook shared in multiple threads. As a workaround, you should use separate workbooks in multiple threads and then combine them into a single workbook.

Please see the following article that explains how to combine multiple workbooks into a single workbook.


Hi,
I have a similar question. I am converting my excel sheets to HTML using ExportActiveWorksheetOnly option as true. I want to convert the sheets in parallel. Is it thread-safe? Is it possible to do this conversion in parallel. My workbook is common and I am using different html streams. I got the error once regarding Key being a;ready present in dictionary so I am just confirming.

@shrey,

If you do not update the same file/workbook in multiple threads, then you may use in parallel threads. But it would better you use different workbooks in different threads.

You may share your sample code (runnable) with template file(s), we will check it soon.