• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: about implementing the timeout of lock

 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all:

i have a problem about implementing the timeout of lock in Data
because my Data is not single ,so i uses static block to start the Timer

but it is error, the notifyAll() can't in static area. what should i do?
Thank you much in advance!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your wait() code look like? The wait() and notify()/notifyAll() methods should use the same instance, which is the monitor. If your wait() code says
foo.wait();
then the notifyAll() should probably be
foo.notifyAll();
Except that since this code is located in a different place than the wiat(), you may have a different name for the reference. E.gg. maybe in this code it's called bar, where foo == bar. Then
bar.notifyAll();
is OK instead.
Note that once you identify the monitor instance, you will also be required to synchronize on that instance in order to call notifyAll(). Otherwise you will get an IllegalStateException.
If your wait() code just says
wait();
then you should consider it to be
this.wait();
and you must figure out what "this" is in the class you're talking about. And how can you refer to the same instance in the notifyAll() code? There are a lot of different ways to do this, so it really depends on how you've got the wait() set up. I can't say much more without seeing that code.
 
Tony Collins
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why implement timeouts ? Too Complex. A good sugestion was have a static weakhashmap to put all Locks in and an instance map to put a instance of datas own locks in. When a client disconnects holding a lock, it' instance locks are GC, and the locks are then deleted from the weakhashmap.
Tony
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jim ,thank you reply
my wait() as
i have change my Data to single one, but still have problem for the notifyAll()
because this notifyAll() is in a new Thread how to notify the Data object? i have try Data.this.notifyAll() and Data.singleData.notifyAll() that the singleData is the only object of Data, it is static
what should i do?
hi Tony thanks
but i have use the Unreferenced interface to unlock the locks of dead client
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the lock() code you showed part of the Data class? If so, then whatever instance you wait() on is the one you have to ntifyAll() with. It sounds like Data.singleData.notifyAll() is probably what you want, or close to it. (Data doesn't need to be a singleton, but if that's what you've implemented, go ahead and access the singleton.
Try adding some logging before each wait/notify, e.g.
System.out.println("About to wait using monitor: " + monitor);
monitor.wait();
and
System.out.println("About to notifyAll() using monitor: " + monitor);
monitor.notifyAll();
where "monitor" can be whatever you're calling the monitor - Data.singleInstance or whatever. The point is, make sure both wait() and notifyAll() are using the same monitor.
And Tony is right - you don't need to implement timeouts, and if you're having trouble getting it to work correctly it's probably a bad idea; consider skipping it.
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you ,i see
but you say "Data doesn't need to be a singleton," why , the database file is only one and if the Data not only one, then if one client is reading, another client is update (he still can update, because the record is not locked), they are two threads with two Data objects. trouble!
maybe i don't need timeout, but i is afraid that the locking is 80
this thread
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is really unrelated to the timeout issue; I probably shouldn't have brought it up here. Basically I think that while the current design does need only one instance of Data, forcing it to be a singleton is unnecessarily limiting with respect to possible future enhancements to the system. Unless you find some tangible benefit to enforcing the singleton nature; perhaps you do. (Some others here will agree with you.) Proably you should search for the word "singleton" on this forum; the vast majority of the discussion you find will be about whether Data should be a singleton or not. Try searching on "multiton" too, that will lead to some good points.
 
ZhiYuan Lin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
o
i see
thank you again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic