• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Some questions about data.java in Assignment

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just received SCJD assignment, and my subject is Bodgitt and Scarper. I've some questions in dealing with the Data.java.
I use an ArrayList as a cache to store the records, a Vector to store all the field names because I will use JTable(Vector, Vector) to show the records, a short[] to store the length of each field(Although I know the lengths are 32,64,64,6,8,8, I prefer to get these infomation from db file.).
When writing the db file in create/update/delete, can I skip to the pos I want to write my record. Or only to clear the file, and write all the data again?


// Returns an array of record numbers that match the specified
// criteria. Field n in the database file is described by
// criteria[n]. A null value in criteria[n] matches any field
// value. A non-null value in criteria[n] matches any field
// value that begins with criteria[n]. (For example, "Fred"
// matches "Fred" or "Freddy".)
public int [] find(String [] criteria) throws RecordNotFoundException;



It must allow the user to search the data for all records, or for records where the name and/or location fields exactly match values specified by the user.


Whether I should separate and/or into two parts or combine them, I mean only to find the records whose name or location matches the criteria? I don't understand why there use a array, only a String is enough, I pass in a condition from a JTextField, or use two JTextFields stands for name and location.


Locking
Your server must be capable of handling multiple concurrent requests, and as part of this capability, must provide locking functionality as specified in the interface provided above. You may assume that at any moment, at most one program is accessing the database file; therefore your locking system only needs to be concerned with multiple concurrent clients of your server. Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available.


I plan to use a WeakHashMap in my Data.java. To lock a record, I put a Key into the WeakHashMap, of course unlock is to remove the Key. I think only the create/update/delete method need lock. First lock a record, do the operation, and then unlock it. I wonder whether I should do lock/unlock in create/update/delete method or do it in the method invoke them in Model. My solution is in the c/u/d method I first invoke lock()-->in lock() use a dead loop-->if the record is locked, then sleep(100)-->when the record is unlocked, break the loop and lock the record-->do the operation-->unlock the record. Does it meet the requirement?
As it is said in the Instruction, I must provide a multitask server, if I don't use synchronize, I can't guarantee the safe of the system. For example, two threads want the same record, the first one check the lock list, the record isn't in the lock list, just before locking the record, the second thread check the list. I am not familiar with multithread, could somebody show me a way or give me an example how to prevent this situation?
[ March 19, 2004: Message edited by: David Ding ]
[ March 19, 2004: Message edited by: David Ding ]
[ March 19, 2004: Message edited by: David Ding ]
[ March 19, 2004: Message edited by: David Ding ]
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David, Welcome to SCJD forum

Originally posted by David Ding:

When writing the db file in create/update/delete, can I skip to the pos I want to write my record. Or only to clear the file, and write all the data again?

I think it is OK to skip to the position while updating a particular record instead of reading all the file. Say if you want to update 7th record, you can skip/seek the file pointer and move to 7th record and update the db file. To clear the file and write all the data again is not a good option, I think.

I plan to use a WeakHashMap in my Data.java. To lock a record, I put a Key into the WeakHashMap, of course unlock is to remove the Key. I think only the create/update/delete method need lock.

I think this is where you need locking i.e. basically while writing instead of reading. But how can you lock a record while creating a new record?

First lock a record, do the operation, and then unlock it. I wonder whether I should do lock/unlock in create/update/delete method or do it in the method invoke them in Model.

I don't know how we can use lock/unlock methods in c/u/d methods. But my understanding is that many people use lock/unlock in adapter or on the server like that.
One solution you can consider is to use the following sequence in either adapter/server


My solution is in the c/u/d method I first invoke lock()-->in lock() use a dead loop-->if the record is locked, then sleep(100)-->when the record is unlocked, break the loop and lock the record-->do the operation-->unlock the record. Does it meet the requirement?

Frankly I did not understand what's going on here. But I have a question for you. If your assignment has a instruction like this: "Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available" then, I think calling sleep will waste CPU cycles. I think you need to use wait, which gives up the CPU.
Phil/George Am I right?

As it is said in the Instruction, I must provide a multitask server, if I don't use synchronize, I can't guarantee the safe of the system. For example, two threads want the same record, the first one check the lock list, the record isn't in the lock list, just before locking the record, the second thread check the list. I am not familiar with multithread, could somebody show me a way or give me an example how to prevent this situation?

I think this is where we need to use the lock/unlock methods. One solution is to use the above protocol lock, doSomething(update/delete), unlock in an adapter or server. In the lock method you check if the record is already locked or not. If the record is already locked, you wait for the lock to be released. The lock is released after the lock's owner call unlock method. Now this notifies those which are waiting for the lock to be released. These other threads try to get the lock again and only one thread will get the lock. Again all the other threads go to wait state. And it follows...If I am not still clear, you can check the following post where locking is discussed in length.
Locking - Notification issues

[ March 19, 2004: Message edited by: David Ding ]


If you have any questions, please ask them. Good Luck.
 
David Ding
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for replying my questions.
I looked thorough the API of JDK1.4 about the DataOutputStream and RandomAccessFile, I didn't find and method like skip() in DataInputStream. How can I fulfill the task?
In my cache, I only make a note that the record is deleted, the instruction require me to use the space of a deleted record, So I think I should lock the deleted record in the cache, modify the content and the delete flag, then write these infomation to the db file. Perhaps you are right, I don't need to do so, I will think about it later.
I used to invoke lock() and unlock() in the create/update/delete method, now I will change my code, let the DbModel to do the job of lock() and unlock(). Maybe this way is better.
I've read the Notification issues you give, and I think I will choose to use "1.2 Synchronization on the locks container / notifyAll() in unlock() only" because it is much easier for me to understand and implement.
 
Satish Avadhanam
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David

Originally posted by David Ding:
Thanks for replying my questions.
I looked thorough the API of JDK1.4 about the DataOutputStream and RandomAccessFile, I didn't find and method like skip() in DataInputStream. How can I fulfill the task?

You may want to take a look at seek() in RandomAccessFile. For DataInputStream, you have a couple of methods to look at: public long skip(long n) in DataInputStream inherited from FilterInputStream and skipBytes(int). Also there is similar method in RAF.

In my cache, I only make a note that the record is deleted, the instruction require me to use the space of a deleted record, So I think I should lock the deleted record in the cache, modify the content and the delete flag, then write these infomation to the db file. Perhaps you are right, I don't need to do so, I will think about it later.

OK. Don't want to let down your thoughts. If you have any questions, you can ask them anytime.

I used to invoke lock() and unlock() in the create/update/delete method, now I will change my code, let the DbModel to do the job of lock() and unlock(). Maybe this way is better.

I meant I did not understand the way you did it. Maybe it is better, maybe not. I don't know. If you feel yours is OK, you can stick with it. If you think there are issues and you want a different design, you can change it.

I've read the Notification issues you give, and I think I will choose to use "1.2 Synchronization on the locks container / notifyAll() in unlock() only" because it is much easier for me to understand and implement.
OK. Whichever you feel comfortable using. Remember if there are any issues that are not dealt with, you need to document them accordingly. For example, if you are not dealing with multiple-locks/ deadlocks you have to specify in the choices accordingly.
FYI: Did you read the line below 1.2 in the thread by Phil. Here's the quote again: Better than 1.1, because CPU cycles are only wasted in unlock(). But still far from fulfilling the instructions perfectly.


Good Luck.
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,
Welcome to JR and this forum.


David:
I've read the Notification issues you give, and I think I will choose to use "1.2 Synchronization on the locks container / notifyAll() in unlock() only" because it is much easier for me to understand and implement.
Satish:
OK. Whichever you feel comfortable using. Remember if there are any issues that are not dealt with, you need to document them accordingly. For example, if you are not dealing with multiple-locks/ deadlocks you have to specify in the choices accordingly.
FYI: Did you read the line below 1.2 in the thread by Phil. Here's the quote again: Better than 1.1, because CPU cycles are only wasted in unlock(). But still far from fulfilling the instructions perfectly.


David, feel comfortable with solution 1.2.
Instead of "But still far from fulfilling the instructions perfectly", I should have written "But still far from fulfilling the letter of the instructions perfectly".
Further in the same thread, I concluded (from an analysis of published scores) that all three ways of doing could lead to a perfect score, though I still think that with 1.1 solution (notifyAll() from lock()), it can be by pure luck.
But 1.2 is valid for sure, and a vast majority of people here use it successfully.
And still in the same thread, I even proved that 1.2 (notifyAll() in unlock()) only was faster that 2.
Regards,
Phil.
[ March 19, 2004: Message edited by: Philippe Maquet ]
 
Forget Steve. Look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic