Forums Register Login

logic for lock

+Pie Number of slices to send: Send
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;
}
}
}
}
}
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
pass it thru an additional constructor.
or u want a soln for getting unique client id ( ip addr) or something ?
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 531 times.
Similar Threads
implementing database lock
record lock&unlock
need lock() be synchronized?
Need your help -- IllegalMonitorStateException
Locking Question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:41:32.