jQuery in Action, 2nd edition*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX: Help, how to notify all Data objects in unlock()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX: Help, how to notify all Data objects in unlock()?" Watch "NX: Help, how to notify all Data objects in unlock()?" New topic
Author

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

ZhiYuan Lin
Ranch Hand

Joined: Jun 30, 2003
Posts: 44
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

Joined: Jun 30, 2003
Posts: 44
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

Joined: May 23, 2003
Posts: 1297
dont put final infront of Map,
why use HashMap ?
why not use HashTable?


BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

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


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Tony Collins
Ranch Hand

Joined: Jul 03, 2003
Posts: 435

[ August 02, 2003: Message edited by: Tony Collins ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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 ]

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
ZhiYuan Lin
Ranch Hand

Joined: Jun 30, 2003
Posts: 44
thank you , especially Andrew
i find where i is wrong, i forget locks.wait() in a place, and just wait()
 
jQuery in Action, 2nd edition
 
subject: NX: Help, how to notify all Data objects in unlock()?
 
Similar Threads
About:My URLyBird1.3.2 Locking
lock/unlock
NX: Exam review (URLyBird 1.1.2) !!!
Cookie value used in DBAccess Interface
lock/unlock review