wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes My Locking Approach  -- Comments Please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "My Locking Approach  -- Comments Please" Watch "My Locking Approach  -- Comments Please" New topic
Author

My Locking Approach -- Comments Please

Animesh Saxena
Ranch Hand

Joined: Jan 15, 2006
Posts: 62
My approach is similar to the one given in Andrew's book. I guess using the Data instance to identify the owner is the best way. I have not used a lock time cos I think it will be outside the scope of the assignment. Also my requirements say that "If a resource is already taken .....the thread should give up the CPU....."
bookings is an arraylist
two locks are used and one is released when the second lock is acquired. But at any time between these.....it is not possible that thread loses both the locks. (After second lock and first unlock)


Let me know if I am wrong....
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi Animesh,

Your lock mechanism looks reasonable.
But I still have two questions :
1.How you implement the isLock method ? (if you have one)
2.Why you catch the the InterruptedException outside of your spin look (while loop) ? Using this someone(a malicious/junior programmer) can unlock a locked record without unsing unlock method.


Regards,
Mihai.


SCJP, SCJD, SCWCD, OCPJBCD
Animesh Saxena
Ranch Hand

Joined: Jan 15, 2006
Posts: 62
Thx for the comments.

isLock method just checks the bookings ArrayList.
It contains all the record numbers which have been locked. If it contains the record number passed to isLock method, it returns true.

Ya ur point on while loop is absolutely correct. Thx Again!
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi Animesh

About isLock method, I presume that you use the entryLock to guarantee synchronize access to it, but how you use it (if you use it) ?


Regards,
Mihai
Animesh Saxena
Ranch Hand

Joined: Jan 15, 2006
Posts: 62
entryLock is declared as

private static Lock entryLock = new ReentrantLock();
Josephx Rainerd
Greenhorn

Joined: Apr 27, 2006
Posts: 12
Are you not using the methods in a synchronized context ??

If so, what are you synchronizing on?
Animesh Saxena
Ranch Hand

Joined: Jan 15, 2006
Posts: 62
-----------
Are you not using the methods in a synchronized context ??

If so, what are you synchronizing on?
-----------
entryLock.lock() synchronizes on this instance. Remember there is no need for a synchronized block when we are using Reentrant locks. Similar to placing

synchronized(this) {
//
}
I just need to access an ArrayList present in this class. And i don't want it to get changed while I am accessing it so I syncronize on this instance. And since this class does not contain much data (Just for locking only!) so synchronizing this instance does not make much of a difference.

Wel any suggestions if its wrong???

Animesh Saxena
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi

Your method is correct but unfortunately can fail by usage. This a classic problem with the test method in multi thread environment - consider the example :


and the usage is :



We play with two threads A and B. Thread A checks if the test() condition is true and it fid it true so Thread A moves from line if (test()) to doTrue(). At
this point the thread scheduler suspend Thread A and runs the Thread B, this thread change the variable condition checked with test() method. After this t
hread A comes back and execute doTrue(); even if the test() method return false.

Regards,
Mihai
Animesh Saxena
Ranch Hand

Joined: Jan 15, 2006
Posts: 62
Thx for the advice.

It should be a while loop. Same point is made in andrew's book!
Thx anyway.
 
Consider Paul's rocket mass heater.
 
subject: My Locking Approach -- Comments Please
 
Similar Threads
[locking] Hand over hand, remove() ?
Multiple Notification Points
lock & read or just read?
delete method code
How do my locking mechanism look?