aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes thread safety and synchronized. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "thread safety and synchronized." Watch "thread safety and synchronized." New topic
Author

thread safety and synchronized.

Abhinav Anand
Ranch Hand

Joined: May 02, 2002
Posts: 113
Hi guys,
Thead safety is an important issue in the server design. Do you guys really think that making a method like add, modify, delete synchrnoized will prevent the data from corruption. Each of these methods move the file read pointer in the db.db database to an appropriate position to read/modify/add a record. Suppose just after moving the record pointer the thread doing the operation was preemted and other thread invokes the criteraia find method to search for a critera. The criteria find method reads data from a different part of the file and exits. Now the original thread modifies/deletes/adds the record at the present file read pointer and exits.
Do you think that the data written by the thread 1 will be a valid one and at a valid position. I don't think so.
Any suggetions....
Thanks,
Vishal Sinha.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
As long as the unit of transaction is limited to one method (in your example: add) and if the method is synchronized, the data will not get corrupt. This add() method may call more private or public methods. That is fine.
But in your unit of transaction, if *YOU* call more than one method in the Data class, then you need to think about wait() and notifyAll() methods and getting the lock for the monitor object.
Abhinav Anand
Ranch Hand

Joined: May 02, 2002
Posts: 113
Hi,
In my implementation I am providing access to the Data class through a sigleton pattern i.e. only one instance of Data class is created and is bound to the rmiregistry. So each and every client will have the same copy of the Data class.
This is the main reason for my concern because since only once instance of Data class is being manipulated by all the clients so multiple threads invoking different methods of the Data class a given moment is very much possible.
Right now I have modified all the methods of my data class to include an additional synchronized statement like:
public synchronized void add(xxxx) {
synchronized(db) {
xxxxxx
xxx
}
}
This will ensure that even though different methods are invoked at the same time. The thread which will have a monitor over the db instance will be allowed to proceed.
I know it seems akward to modify all the data class mehtods but the assignment instruction balatantly states that "since multiple client connections can exist, you should make your Data and xxxxx classes thread safe".
Any suggetions are welcome.
Thanks.
Sai Prasad
Ranch Hand

Joined: Feb 25, 2002
Posts: 560
If you bind the instance of Data to the RMI Registry how can you make sure that Client B is not allowed to unlock the record locked by Client A?
Matt Ghiold
Ranch Hand

Joined: Feb 24, 2002
Posts: 213
One other thing, is if you bind your data class to your rmi registry, how are you planning to handle the local version of the app. The instruction's say that no sockets may be created during local mode, and if you tie it to rmi, you are guranteed at least 1 socket.
-Matt


-Matt<br /> SCJP2<br /> SCJD
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

I think you will want to look at another solution other than binding the Data class to the RMI Registry. Without giving the answer away. Do a search on RMI or Connection.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Originally posted by Vishal Sinha:
In my implementation I am providing access to the Data class through a sigleton pattern i.e. only one instance of Data class is created and is bound to the rmiregistry. So each and every client will have the same copy of the Data class.

Since the binding is performed by the server once when it is started, isn't the bound object a singleton by definition? All clients will get it from the registry, and it is the same object, isn't it? What is the need to make it a singleton?
Eugene Kononov.
Abhinav Anand
Ranch Hand

Joined: May 02, 2002
Posts: 113
Hi guys,
Actually I did'nt want to create 10 different instances of Data and its networked conterpart RemoteData for 10 different client.
The main reason is
1. I have implemented the lock & unlock logic in the Data class instance itself. So by making the Data class for a database file as singleton my data class's lock method can block client's lock request and reject illegal unlock request.
2. I wanted a compltely serialized access to the Data class. So as i mentioned above that each of the methods of the Data class have to obtains a mutex lock of the db instance beofore they can access it directly. So no two instance can access or manipulate the database at a given time. Finally this the main reason as to why I preferred a singleton approach to the Data class.
Simply why to delegate the lock and unlock responsibility to a different class when it is working effectively in one class. And finally why to make 200 instance of data class and one instance of Lock manager when ultimately I have gurantee a simple serialized access to the Data instance, which is effectively being done through a singleton.
Any suggetions are welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread safety and synchronized.