• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do I really need synchronize lock method?

 
Po Sun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi you all guys.
I'm nearly finish my assingment, but still have a question puzzle me when
checking my locking mechanism.My implemention at server side only one instance of data class,and all clients share same refrence of data object through RMI.Currently, i synchronize both lock and unlock method.
My confusion is:
If synchronize a method, that means only one thread can access it at a time.Suppose a thread enter the lock method and wait a former thread to unluck a record,meanwhile,no other threads could access lock method.But our purpuse is allow all threads enter lock method and wait(if need) rather than wait outside lock method.On the other hand, multi-thread access single method may arise issues.What i should do or any other better solution?

Any comment?
[ November 01, 2004: Message edited by: Po Sun ]
 
Manik Saha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My implemention at server side only one instance of data class,and all clients share same refrence of data object through RMI.Currently, i synchronize both lock and unlock method.
My confusion is:
If synchronize a method, that means only one thread can access it at a time.Suppose a thread enter the lock method and wait a former thread to unluck a record,meanwhile,no other threads could access lock method.But our purpuse is allow all threads enter lock method and wait(if need) rather than wait outside lock method.On the other hand, multi-thread access single method may arise issues.What i should do or any other better solution?

Any comment?


Even I am doing the SCJD, so here's my opinion.

This solution is inefficient since clients wanting locks on other records have to wait. Actually this could be as bad as having a synchronization where the record number does not matter. Eg. Thread 1 takes the lock on record 1, Thread 2 tries to acquire the lock on record 1 but goes to wait inside sync. All other threads are blocked outside the sync method irrespective of the record number.
 
Jared Chapman
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What about trying this:


[ November 01, 2004: Message edited by: Jared Chapman ]
[ November 01, 2004: Message edited by: Jared Chapman ]
 
Inuka Vincit
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definitely synchronizing methods is a no no. The only method that should come under that maybe Create depending on your implementation. It doesnt make any sense to do so because we aleady have locks to deal with it, if we synchornize methods locks become redundant.

only synchronize on the lock (object) on the method.
the psudo codeish stuff goes like
synchronize(lock){
while(lock is locked)
{
//try catch exceptions
wait(lock); //wait on lock

}
lock the lock;
}

the code depend on the implementation. From what you stated sounds like your confused as to the whole locking issue. I sugest that you search and read some of the past discussions regarding locks to get a better idea about implementations.
[ November 01, 2004: Message edited by: Inuka Vincit ]
 
Po Sun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your reply!
It seems my previous implemention on lock method like a pipe, one record blocks in it,all records can't through it.That's low efficiency.Ok,I will take the point you said into consideration and rebuild my lock method.
Thank you again!
[ November 01, 2004: Message edited by: Po Sun ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic