Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: Help, how to notify all Data objects in unlock()?

 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all:
i don't use singleton pattern, my Data give each remote client a new object, when i test the Data, find that if a record is locked , then other remote client will be blocked forever.
my unlock()

my lock() is

how to notify all the Data objects
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private static final Map locks = new HashMap();
i have try :
synchronized (locks) {
//....
locks.wait();
}
and
synchronized (locks) {
//....
locks.notifyAll();
}
but it is still unworking!
what should i do?
 
Billy Tsai
Ranch Hand
Posts: 1304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dont put final infront of Map,
why use HashMap ?
why not use HashTable?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11852
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ZhiYuan
In the first example you gave, you were synchronizing on the instance of Data, which was different for each connected client. So calling notifyAll would only notify threads waiting on that particular instance of Data - in other words no other waiting thread.
Your second post indicates that you tried having a static object to wait and notifyAll on. This is correct. I do not know why it didn't work for you. Perhaps you could post your locking and unlocking code with the synchronization on the static object so we can see that? Making the object final does not stop the wait / notifyAll from working (however I don't see much point to having the final there).
Here is some code (including the final) so you can see that the wait / notifyAll should work:

Billy - you asked about Hashtable and HashMap. Hashtable is part of the old collections framework (which is why the name doesn't meet the naming conventions), and is synchronized. Since the locking mechanism needs to be synchronized itself, using Hashtable has an unnecessary performance penalty. So unless there is a specific reason for wanting to using a Hashtable I would recommend against using it. For the code given, HashMap is a better choice.
Regards, Andrew
 
Tony Collins
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ August 02, 2003: Message edited by: Tony Collins ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Billy Tsai:
dont put final infront of Map,
why use HashMap ?
why not use HashTable?

Actually Billy. HashTable is considered older Collection, and a HashMap is more desirable. HashTables were synchronized and therefore slowed them down. With a better design of HashMap to implement the Collections interface, they have upscaled this Collection to be better and preferred over HashTable.
Oh, and well said Tony.
Mark
[ August 02, 2003: Message edited by: Mark Spritzler ]
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you , especially Andrew
i find where i is wrong, i forget locks.wait() in a place, and just wait()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic