Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

logic for lock

 
Reshma Das
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
someone please check my lock logic and let me know if there are any flaws.
boolean lock=true;
public void lock(num) {
synchronized(vector) {
if( (vector.containsKey(new Integer(num))) ||
(vector.containsKey(new Integer(-1))) ||
(! lock)) {
try {
vector.wait();
} catch(InterruptedException ie) {
}
while (lock) {
while((vector.size() > 0) & (num == -1)) {
lock = false;
try {
vector.wait();
} catch(InterruptedException ie) {
}
vector.put(new Integer(num), "true");
//notifyAll();
break;
}
}
}
}
}
 
Bal Sharma
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks good to me expect on thing. Vector holds your list of of lock right! If so, use other data structure instead of Vector. Vector is synchronized. You are putting synchronized object in synchronized block.
-Bal
 
Brandon Nguyen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
I was looking at many lock/unlock designs discussion in this forum and still couldn't find a way to pass in clientID when calling lock. I have a similar as yours but there is no notion of clientID. If I have something like:
public class ServerImpl extends UnicastRemoteObject implements IServer
{
public void reserveSeat( clientID, flightID, numSeats )....
{
lock(recordNo);
read(...);
modify(....);
unlock(recordNo);
}
}
public class DataExt extends Data
{
public void lock(int record)...
{
}
}
Can you give me some pointers on how to pass clientID to DataExt?
Thanks in advance,
Brandon
 
Reshma Das
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pass it thru an additional constructor.
or u want a soln for getting unique client id ( ip addr) or something ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic