Is Thread Safe Object Pool guaranteed to return an existing object if strong links exist?

I am trying to extend the Clojure language to extend ACI-guaranteed links to ACID-guaranteed drefs (long links). The API should simply call (dref key value)where keyis the key string to be used in the underlying data store (BDB JE in my current implementation), and valueis the object that dref needs to initialize. If keyalready exists in the database, the stored value is used instead.

Several drefs can be created with the same key, and they must be synchronized, that is, if one dref with key "A" is involved in a transaction where it is written or read using (ensure), all other drefs with key "A" must be synchronized by transactions: to lock read locks and write locks, you must enter the order of transactions associated with these drefs. In a broader sense, although there can be more than one dref in the same document in memory with the same key, all these drefs with this key are one logical entity.

For obvious reasons, it is much simpler to simply ensure that this single logical dref is implemented using one specific dref in memory. Therefore, do not sync anything. How to do it?

The obvious answer is to use a pool of objects with a key. Then Clojure will call a static method getInstance(key,value)to retrieve from the pool, if it exists, and create it and fill the pool if not. The problem with this approach is that there is no easy way to get Clojure before the release of an object when it is executed. City of memory leak. I have to guarantee that any object with strong references to it will not be assembled and that they exist in the pool. It would be disastrous if the pool loses references to logical drefs that are still in use, since another process can create a new dref with the same key, and it will not be transactionally safe with another dref with the same key.

WeakHashMap -, - ( SoftReference GC). :

  • HashMap<String,SoftReference<DRef>>, , , (SoftReference)? - ?
  • GC? , GC SoftReference, , Map?
  • , , ? , , , JVM, ? , , ?
+3
3

Google ?

MapMaker, / . , Weak/Soft key , , , .

(org.apache.commons.collections, ).

+1

Psssst.... Terracotta . Terracotta , ( DSO) - , , Clojure .

Terracotta, ClientObjectManager (http://svn.terracotta.org/svn/tc/dso/trunk/code/base/dso-l1/src/com/tc/object/) , . pojoToManaged TCObjectImpl.

1,2). , , - , . SoftReferences GC ( ) .

3) Google ...

+1

, , Collections.SynchronizedMap(new WeakHashMap()) - .

1) Map<K, V> ConcurrentHashMap<K, SoftReference<V> >. SoftReferences ReferenceQueue , ReferenceQueue / ( n- ..).

2) GC - , , .

3) You can see how AWT-EventQueue is managed. But:

- your demon flow is likely to be simple enough not to throw unexpected exceptions

- if you are worried about this, you can wrap the meat of your demon’s stream in

for (;;) {
  try {
    //daemon thread loop here
  } catch (Exception ex) {
    //log it, any other possible cleanup
  }
}

which will work forever if you do not receive an error message (in this case you have more problems.)

0
source

Source: https://habr.com/ru/post/1765656/


All Articles