1

I am developing a "Virtual Bank" for my Computer Network class. But, while developing the server, we cannot use databases. We can only use system files (like .txt). So, how can I deal with file concurrency in an efficient way?

edit: It's possible to have shared accounts

  • What do you mean by file concurrency? Do you want to be able to write to the same file from different threads/processes? – SHG Apr 10 '17 at 23:34
  • Possible duplicate of [Is java's File class' object serializable?](http://stackoverflow.com/questions/25073435/is-javas-file-class-object-serializable) – Mohammed Housseyn Taleb Apr 10 '17 at 23:34
  • 2
    @MohammedHousseynTaleb That's not a duplicate. – xiaofeng.li Apr 10 '17 at 23:37
  • you may also found a solution using file Lock in [nio](http://stackoverflow.com/questions/128038/how-can-i-lock-a-file-using-java-if-possible) – Mohammed Housseyn Taleb Apr 10 '17 at 23:37
  • @SHG, I want to store accounts. So, I'm trying to avoid problems like, say I've got 20 bucks on my account, but my wife (has the same account as me) tries to get that money in the same time I do. I know of synchronized methods, but applying it to everyone would make the system really slow – Victor Munduruca Apr 10 '17 at 23:40
  • try to create a pool writing system that have watch, when you connect to the server the server gives his local watch time to all client every client pull a request in a server thread pool that is timed ( you can use lamport or ricart and agrawala to do so, there is lot of time based algorithms ) then the server will consume from pool every let say 50 millisecond (the server will do a sort by time then will consume all thread sequentially ) – Mohammed Housseyn Taleb Apr 10 '17 at 23:45
  • @MohammedHousseynTaleb That's not really a duplicate. But, that file lock solution seams interesting, if I create a .txt file to each account and each person. – Victor Munduruca Apr 10 '17 at 23:47
  • using RMI may make it easier for you, that what I ll try to do in your place – Mohammed Housseyn Taleb Apr 10 '17 at 23:47
  • When you want to synchronize it's always getting slower, but why do you think it will be **really** slow? A possible way would be to create a message queue to read/write from the account and then only one thread/process is executing actions according to the queue. – SHG Apr 10 '17 at 23:49
  • I do retract my flag, I tired I did a mistake to think that is duplicate. also the lock is one possible solution using a server synchronised write method to only one file or multiply your files, there is lot of solution that depends on your strengths – Mohammed Housseyn Taleb Apr 10 '17 at 23:49
  • that will help you to do what SHG asked you to do :[here](http://stackoverflow.com/questions/10006647/java-concurrency-multi-producer-one-consumer) – Mohammed Housseyn Taleb Apr 10 '17 at 23:54
  • @SHG I don't know, my coleagues conviced me it would, I don't really have any proofs about it. It would be somewhat efficient in a multi-client scenario? – Victor Munduruca Apr 10 '17 at 23:57
  • That's the tradeoff, limitations have consequences so either way, you'll pay, regardless of the method you pick. Regarding your task - I don't know what's the scale of your system so I can't answer that. If you're a real bank or your clients are quick trading algorithms or something similar, then you probably have to implement it really efficiently. Otherwise, probably a message queue will do. – SHG Apr 11 '17 at 00:30
  • [Locks are not expensive, lock contentions are.](http://stackoverflow.com/questions/9012116/what-is-the-reason-for-locks-are-an-expensive-operation-to-be-uttered-so-often) – xiaofeng.li Apr 11 '17 at 00:51
  • Also. Are you required to support transfer between accounts? Be careful with deadlocks when locking two accounts. – xiaofeng.li Apr 11 '17 at 01:04
  • @SHG OK, I understand you, those things really happen either way – Victor Munduruca Apr 11 '17 at 02:45
  • @LukeLee Yes, transfer between accounts is a requirement. Thank you for the tip! – Victor Munduruca Apr 11 '17 at 02:46

0 Answers0