• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: About read and write lock mechnism

 
Jamy Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just lock records by one kind of lock mechnism. But I think use two kind of lock mechnism ,read and write lock, will be better, am I right?
I want to know if I just use one kind of lock, will I get high points of this exam(especially locking part)?
If I want to realize read/write lock, Could someone tell me how to do it?
Thanks a lot!!
 
Leo Tien
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi james:
What's two kind of lock mechnism is ?
 
Jamy Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, I didn't describe it clearly.
I mean I just have one kind of lock. Not differ read lock from write lock!
For example, when I read a record, I just call read lock, other threads can read the record but can not write it.
 
Leo Tien
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi james:
I disagree with you, lock is related with a object, not a read/write method. What read/write means :
1) It points Data's read()/write() methods, if this, I sugest you use one lock. If you use singleton Data design, then use Data's lock simply. Otherwise in multiple Data instance design, you can use other singleton class such as DataHelp or DataSchema. This ensure that at one time these's only one thread do read or write operation on a file(the db file), this will prevent your server wouldn't be crashed. I remember Vlad had said that in UNIX only alows less than 500 threads to access one file.
2) It points 'read/write' operation on a record. At this condition, 'read' will be allowed forever, but 'write' is blocked use locking mechanism. To do this, you can use lockedRecordContainer. Every thread want to do 'write' on a record, it muse hold this record in lockedRecordContainer first and only this thread can release the record later. At last, I mean no lock between 'read' and 'write'.
This is my personal idea, what do you think ??
 
Jamy Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leo, thank you for your reply.
Do you mean we don't need to lock the database when read? If not, when we read, we lock the records. Then other clients can not read the records until we finish reading. Maybe it will be not efficient enough?
 
Leo Tien
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi james:
If you use singleton Data instance, this will happen.
 
Jamy Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't use singleton Data instance.
 
liqun chang
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi james,i see this thread,and i want to give you some suggestions.Hope this is helpful.From my feeling(i am empressement),you need to learn about
the thread from sun site or from java book.and i had seen about read/write lock in www.javaworld.com could help you have clear ide about multithread.
After finish this,you can get the correct result.In fact,this is the best
way to go,and consume a little time on it. If you finish rmi,multithread,and swing and RandomAccess and ood,then you can begin your
design and ask some questions.because your question is clear and simple ,then every experts will be interested in it.so do,you can be clear
more and more until to finish you design.
Hope this is helpful.
[ February 14, 2004: Message edited by: liqun chang ]
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12007
215
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
I don't believe it is necessary to have read locks.
The read operation itself should be atomic, but once the record has been read, any other client should also be able to read and/or update it.
If you allowed read locks, then you could have the situation where one client reads the entire database, after which no other client can book any record until the first client releases their read locks.
Of course without read locks you will have to check that the record is still valid when you go to update it.
Regards, Andrew
 
Jamy Wang
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew, thanks a lot!
I tend to what you said. Even if I don't have read locks. It's also thread safe. But I think if I have read locks, my code will be better.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic