• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized on method or object

 
Ray Cheeny
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
1. I know synchronized on method or object both will work fine .But which one will performance better on earth? Which one did you choose?

2. when lock -1,I wait all the client which have had lock ,then lock all.Is that right? When I lock all ,which one should I use?
a.just set a flag
b.set a flag and lock all with the calling object
3. In my referenced(),I clear all the locks.
a.Need I wait for all the clients which have had the lock.Then clear all.
b.make the locks = null ?
or remove every lock one by one?
Thanks a lot!
I know there're a bit more.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ray,
You asked I know synchronized on method or object both will work fine .But which one will performance better on earth? Which one did you choose?
Can't answer the performance issue (well I could, but I dont have time to write the test code to find out ).
I allowed multiple instances of my Data class to exist, all of which could be putting locks into the static Set that holds the locks. So I did not have a single class that I could synchronize the methods. Therefore I synchronized on the object (my static Set).
2: 2. when lock -1,I wait all the client which have had lock ,then lock all.Is that right?
I also pre-empted new locks being created by setting a flag to indicate that a lock all was pending. No new locks were granted during that time.
Otherwise you attempt to lock all. Record 1 is currently locked so you wait. Meanwhile someone locks record 2. Then record 1 is unlocked, but you still have to wait because there are locked records. Then record 1 is locked again before record 2 is unlocked. And so on forever.
When I lock all ,which one should I use?
a.just set a flag
b.set a flag and lock all with the calling object

I didnt want to have a flag to indicate that the database was locked - how do you handle the case where two clients are trying to lock the entire database? OK - not very likely, but it could happen. For instance, someone trying to do a maintenance (change all prices for all flights) at the same time as someone is trying to shut down.
Therefore I locked record -1 to indicate that the database was locked - only one client can ever get this lock (same as any lock).
3. In my referenced(),I clear all the locks.
a.Need I wait for all the clients which have had the lock.Then clear all.
b.make the locks = null ?
or remove every lock one by one?

Do you mean unreferenced()?
You would only want to clear the locks for the client which has disconnected (triggered the unreferenced()) not all clients that are running.
This would indicate removing locks one by one.
Or have I missed your question?
Regards, Andrew
[ May 23, 2003: Message edited by: Andrew Monkhouse ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic