Synchronize on a integer value
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Kasper_Nielsen
Posted On:   Sunday, April 6, 2003 07:58 AM

Hi, I would like to perform some synchronization on long values. I.e. something like long ressourceID=4 (from 0 - 1.000.000.000.000) synchronized (ressourceID) { .... ... } Wrapping it into a Long isn't enough because Long(4) and Long(4) isn't necessarily the same object. Is there any easy way to do it? I thought of creating a hash table to store the objects to be synchronized on and using the ressourceID as key to the object. However a lot of extra synchronization is going into that. I also thought about creating a object pool wit   More>>

Hi,


I would like to perform some synchronization on long values.
I.e. something like



long ressourceID=4 (from 0 - 1.000.000.000.000)



synchronized (ressourceID)


{


....


...


}



Wrapping it into a Long isn't enough because Long(4) and Long(4) isn't necessarily the same object.



Is there any easy way to do it?



I thought of creating a hash table to store the objects to be synchronized on and using the ressourceID as key to the object. However a lot of extra synchronization is going into that.



I also thought about creating a object pool with around 10000 objects being creating at class initialization and then getting objects to synchronize on like getObject(ressourceID mod 10000). This has the drawback that resources with id 10001 and 20001 tries to acquire the same lock. however the program will still function correct.



any ideas, am I missing something ridiculous easy

   <<Less

Re: Synchronize on a integer value

Posted By:   Anonymous  
Posted On:   Tuesday, April 8, 2003 10:26 AM



You've presented a hard problem. I assume you have disjoined parts of your code which need to mutual agreement on the lock for a given "logical" record (your "id"). The easiest approach may be redesign (with singletons for your IDs).



Without a redesign, I think your two suggestions are the only way to go. You may also want to consider the characteristics of your app (which I am not familiar with). Are the odds that the different modules want the same "id" high or low? You can get past some of the Object accumulation issues by changing your semantics from "synchronized" to "get/release lock" on a common class which keeps Objects for each ID in an internal collection. When the lock count goes to zero, you can dispose of the ID in your collection. Of course, you have to be careful of exceptions which would cause you to loose track of your locks (and cause deadlocks).



Either way, I agree with the suggestion to check out Doug Lea's book. He lays this stuff out nicely.



-Bill

Re: Synchronize on a integer value

Posted By:   Edward_Harned  
Posted On:   Sunday, April 6, 2003 11:41 AM

What you are missing is a Language Reference Manual. Get one and read up on synchronization. You can only synchronize on Objects.



The purpose of synchronization is to lock out other threads from accessing a shared resource and to read/write the local resource from/to main memory.




Object lock = new Object();


new myThread(lock).start();

new myThread(lock).start();


In this case, both threads have a reference to the lock Object and when they need to coordinate they can synchronize on that object.


About | Sitemap | Contact