Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

urgent help required on syncronized variable

 
Reshma Das
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam going mad on syncronized stmt. i have a code like
class textlock{
public syncronized reserver(){
System.out.println("lock called");
data.lock();
System.out.println("bookcalled");
data.book();
System.out.println("updatememorycalled");
data.updatememory();
System.out.println("unlock called");
data.unlock();
}
} //end of class
Data{
lock(){
syncronized(arraylist){}
}
unlock(){
syncronized(arraylist){}
}
syncronized book() {}
synccronized updatememory(){}
}
then i wrote a seperate class which implements
Runnable interface . in it constructor, it runs a for
loop
for(i=0;i<30..)
testlock.reserver(fltno,tickets);
the output is so horrible. can someone explain why iam
getting non syncronized outputs. all my methods are
syncronized. iam so confused.

output:
lock called
bookSeats called
26 pendingseats= 8 leftoutseats = 7
lock called
bookSeats called
30 pendingseats= 8 leftoutseats = 7
lock called
bookSeats called
31 pendingseats= 8 leftoutseats = 7
lock called
bookSeats called
updateMemory called
1 pendingseats= 7 leftoutseats = 6
lock called
bookSeats called
2 pendingseats= 7 leftoutseats = 6
lock called
bookSeats called
updateMemory called
updateMemory called
3 pendingseats= 7 leftoutseats = 6
lock called
bookSeats called
updateMemory called
4 pendingseats= 7 leftoutseats = 6
 
Reshma Das
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a doubt my lock() method contains
while(true) {
if vec.contains(recnum)
wait();
else {
add vec.add(recnum);
break;
}
}
if i do this, my current thread releases the lock. a new client gets into lock, the loop continues. there is some deadlock going on. i dont know. iam so confused. can someone mail me some good link for understanding synchronized. iam read many articles but left clueless.
 
Reshma Das
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found that when i add Thread.sleep(1000) within my if loop, the synchronisation works fine. as soon as i remove it , sometimes it works sometimes it fails. i c that the lock method gets called lot of times and since someone is holding the lock, it goes in some infinite waiting loop.
my code is very simple.
This is my lock code:
synchronized void lock()
if(arraylist.contains(new Integer(rec))) {
try {
wait();
} catch(InterruptedException ie) {
}
} else {
arraylist.add(new Integer(recordNum));
}
Iam i missing something? did someone tried load testing ?
 
Reshma Das
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: Iam running a simple loop which creates threads and invokes my server.bookseats(...).
iam passing the same recordnumber to check for concurrent access. and ima stuck with it.
thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic