views:

30

answers:

2

My scenario is : I've an excel sheet. Some threads need to modify it at the same time.

I need to implement this and I'm looking for a best practice solution. synchronizedmethod could be a solution? If yes, then wouldn't it create starvation problem or every thread may have to wait for long time?

Is any other way round?

A: 

You can implement your logic by the system business rule like,

  • You can define priority amongst the Process
  • You can give the operation first to the shorter process.

At last only sync. will work for you but you can make it better using some system analysis

org.life.java
+3  A: 

An Excel sheet shouldn't be used as working memory. With a resource like that, you have to manage access to it. You can't just let threads try to write to it willy-nilly. I doubt you will find a java Excel module which supports concurrency.

Create a data structure which contains all the data. At the beginning of the process, load the Excel sheet into this data structure. Have the threads modify this data structure instead of the excel spreadsheet. Periodically, or when changes are complete, export the data structure contents back to the Excel spreadsheet.

You will have to handle data concurrency issues yourself. There is no native transaction handling here. You want to make sure you don't write a copy of the spreadsheet that one thread has made some changes to, but not its complete set. This could create confusing results. Also, if two threads are modifying the same data at the same time, that would be confusing too.

Ideally, in a situation like this, the data is held in a database somewhere. This is an ideal place to store data that is concurrently modified by multiple threads. Mainly, it also supports transactions. If you go with this solution, you could create a script which just polls the database every five minutes and updates the Excel spreadsheet.

Erick Robertson