• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread safe?

 
Salish Tankard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new here, can someone give me some suggestions if my server side is threadsafe?
I implemented a SortSet to store the locked record number. So, when the record number is added to the SortSet, it means the record is currently locked. Then, I unlock the record by removing the record number from the SortSet. Whoever needs to lock the record needs to check if the record number is currently in the Set, if it is, the client needs to wait until the record is unlocked.
So, the operation will be like:
ClientA->lock (add record number into the SortedSet)->modify record->unlock (remove record number from the SortedSet)

Thanks very much
Salish
[ October 08, 2002: Message edited by: Salish Tankard ]
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Whoever needs to lock the record needs to check if the record number is currently in the Set, if it is, the client needs to wait until the record is unlocked.

Yes, you are on the right track. Just remember about the requirement that client A must not be allowed to unlock the record that was locked by client B. That's what makes it complex, -- you have to come up with some sort of client tracking mechanism.
Eugene.
 
Salish Tankard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just remember about the requirement that client A must not be allowed to unlock the record that was locked by client B. That's what makes it complex

Thank a lot.
But, I am confused about why Client B may be unlock the record locked by Client A? Thanks in advance!
 
Daniel Chen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Salish,
My implementation is as familiar as yours except I use Vector instead of SortedSet.I synchronize lock method and let it wait when the record number exist in Vector.Then I remove the record number from Vector in unlock method and call notify.The advantage is server can bookFlight at the same time if the request book diffient flight.
 
Salish Tankard
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just noticed that both Vector and SortedSet are not threadsafe in javadoc 1.4. So, should it be safer if we synchronize the Vector/SortedSet, instead of synchronize the lock and unlock methods, because I think if we synchronized the methods, it does not gurantee that at the same time multiple clients are not making changes in our record number Vector/SortedSet? Is this what they mean by "not threadsafe" for Vector and SortedSet? I am confused about the what I saw in the javadoc 1.4. Please advice..... Thanks
 
Max Habibi
town drunk
( and author)
Sheriff
Posts: 4118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"As of the Java 2 platform v1.2, this class has been retrofitted to implement List, so that it becomes a part of Java's collection framework. Unlike the new collection implementations, Vector is synchronized"
Javadoc for Vector in 1.4

Vectors not Threadsafe? Since when? Also, in the context of a Vector, to be Threadsafe means to
have your operations happen atomically. So if Thread A does myVector.add("Hello"); and then Thread B does myVector.contains("Hello"), then Thread B will get a true value. This is not guaranteed for ArrayLists, etc.
HTH,
M, author
The Sun Certified Java Developer Exam with J2SE 1.4
[ October 09, 2002: Message edited by: Max Habibi ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic