This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi all, I have defined the Database class that works with the RAF instance. It is a singleton and contains one instance of RAF. Every Data class works with this Database class. I am confused about the correct implementation of the thread safety, in other words synchronization in this class. I know that there are two ways of the synchronisation for my case:
1. synchronize every method that works with the RAF. 2. synchronize in every method the RAF instance.
But I don�t see the difference and what is correct implementation for my case. The only difference is that for the first case only one client has Database class access in a time. For the second one the only one client has RAF access in a time. But I couldn�t understand what the correct implementation is.
Could you help me? Thanks a lot for your Help!!! [ December 27, 2005: Message edited by: Elena Taube ]
well, I guess both of these approaches are correct. However, I'd go with the second one, ie synchronising on raf instance, because it gives you a better control and performance ( eg, before accessing raf in a multi-thread safe way you might want to do something in the Database methods that does not require synchronisation ).
Depends on your assignment, I guess the Thread safety particularly means locking of record, e.g. when a record is being modified by one thread, it cannot be accessed by other threads.
you can do this: have a HashMap, when a thread is modifying a record (like booking the record for some assignments when you need to modify the booked field or owner field), firstly check the HashMap, see if the id of the record(usually record number) is one of its values, if so, yield; otherwise, put the thread itself and the record id as key and value respectively to the hashmap, then modify the record, after modifying, get the key-value pair out from the hashmap -- hope my idea is right, but it's simple anyway, you can have a LockManager that specifically do these.