Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My Locking Approach -- Comments Please

 
Animesh Saxena
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Animesh Saxena
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
entryLock is declared as

private static Lock entryLock = new ReentrantLock();
 
Josephx Rainerd
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you not using the methods in a synchronized context ??

If so, what are you synchronizing on?
 
Animesh Saxena
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-----------
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
Posts: 918
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx for the advice.

It should be a while loop. Same point is made in andrew's book!
Thx anyway.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic