• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJD

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I just want to check with all of you whether what I have developed is correct or not.
1.I have a class that extends the Data class and has the methods lock(),unlock() and criteriaFind(). Of these, the first two methods are synchronized whereas the last one is not. Also, is it required that the lock()and unlock() methods be static as well.And does the third method needs to be synchronized.
2.RMI Server has methods like reserve which is synchronized and that calls lock and unlock() methods.
Also, the sequence that I get for locking is read - lock - write - unlock. Is this ok ?
Waiting for replies,
Palkhi.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Palkhi,
Quick question regarding the class that extends Data class -is that something Sun has provided?
Or it was your brainchild...I don't have any class like that.I am just using Data class in the server to manipulate the data.
Robin
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not get your question clearly. The Data class was provided by Sun and my class extends Data class to implement the three methods, viz lock(), unlock() and criteriaFind(). Is my approach right.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Palkhi:
I did not get your question clearly. The Data class was provided by Sun and my class extends Data class to implement the three methods, viz lock(), unlock() and criteriaFind(). Is my approach right.

That sounds OK. But are the functions lock() and unlock() also available on the server because i remember the assignment specification mention that the functions lock(), unlock() should be callable from the remote client. I do not understand why it should be callable from the client (unless they are just testing us to see if we can translate requirements into code) because locking functions are server responsibilities. Also, what collections or data structures did you use to acheive the lock mechanism?
 
Milind Kulkarni
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think that criteriaFind() should also be synchronized becuase it may be accessed by more than client at a time.
Regards,
Milind
 
krishnan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
precisely lock and unlock should be taken care at the server level.
What i have done is exposed these apis to client, but within data all database write and add methods call lock and unlock internally too. So my client code doesnot use these methods, but to satisfy suns requiements have the apis available too?
any thoughts on this one?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, the lock() and unlock() methods are not available to the client. The client calls a method on the server for reserving the seat and the server method then calls lock() method to reserve the seat.
There is one more doubt. The sequence that I am getting for reservation is read(),lock(),write() and unlock() whereas what is required is lock(),read(),write() and unlock().
I don't know how to achieve the second sequence as I am displaying all the records in the table (equivalent to read()), the client clicks on the reserve button which calls the method on the server which in turn calls lock().
Also, which method needs to be synchronized, the method on the server that in turn is calling lock() or only the lock() method.
Any thoughts on this.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Palkhi:
No, the lock() and unlock() methods are not available to the client. The client calls a method on the server for reserving the seat and the server method then calls lock() method to reserve the seat.

There are two things i am yet to resolve:
i) For the criteriafind function :We have to read the database sequentially. I am using the getRecord method in a loop until EOF. The only problem i find is that it uses seek(recno) and readRecord(). Readrecord will read from whereever the filepointer is. If there are two threads manipulating the file and one has finished seek() and then the next thread reads or writes someother record, then the filepointer will be changed right? So when the original thread does a readrecord() it will read something else. So seek() and readrecord have to happen as an atomic operation. Am i right?
ii) What coolection to use to store the lock details for each record. I decided on a HashMap but am stuck onn synchronising access to it so there is no corruption on concurrency.
Any ideas welcome.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
what you must consider is that by never calling a synchronised method from a synchronised method or object is that you can never cause deadlock. my opinion is that sun will fail you if there is the chance that deadlock will occur in your code. parlikkad is correct in that he wishes to use a Hashmap. that is not synchronised. lock the collection object yourself.
public void lock( int rec ) {
synchronized( collection ){
// change the collection
}
}
create a Hashmap collection that uses the record number as the key and a Boolean object in the content(true if locked). lock the object in the lock and unlock methods when you decide to change it. there is no need to lock the whole data class when you want to lock a record, the performance hit will be to great.
there is also no need to synchronise the criteriaFind() method. the getRecord() is synchronised itself so it will be thread safe itself.
i hope this is of help. if anyone finds this information eronioius i shall welcome input.
dr
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic