19 Ağustos 2019 Pazartesi

Striped Sınıfı

Giriş
Açıklaması şöyle.
There are three flavors of this class: Striped<Lock>, Striped<Semaphore>, and Striped<ReadWriteLock>.

For each type, two implementations are offered: strong and weak Striped<Lock>, strong and weak Striped<Semaphore>, and strong and weak Striped<ReadWriteLock>.

Strong means that all stripes (locks/semaphores) are initialized eagerly, and are not reclaimed unless Striped itself is reclaimable.

Weak means that locks/semaphores are created lazily, and they are allowed to be reclaimed if nobody is holding on to them. This is useful, for example, if one wants to create a Striped<Lock> of many locks, but worries that in most cases only a small portion of these would be in use.
get metodu
Açıklaması şöyle.
The guarantee provided by this class is that equal keys lead to the same lock (or semaphore), i.e. if (key1.equals(key2)) then striped.get(key1) == striped.get(key2) (assuming Object.hashCode() is correctly implemented for the keys). Note that if key1 is not equal to key2, it is not guaranteed that striped.get(key1) != striped.get(key2); the elements might nevertheless be mapped to the same lock. The lower the number of stripes, the higher the probability of this happening.
Örnek
Şöyle yaparız.
Lock lock = keyLocks.get("resourceId")
Örnek
java.util.concurrent.locks.Lock arayüzünü kullanıyorsak şöyle yaparız.
String key = ...;
LOGGER.debug("Locking with Key : " + key);
Lock lock = locks.get(key);
LOGGER.debug("Acquiring Lock : " + lock);
lock.lock();
Şöyle yaparız.
String key = ...;
LOGGER.debug("Unlocking with Key : " + key);
Lock lock = locks.get(key);
LOGGER.debug("Releasing Lock : " + lock);
lock.unlock();
lazyWeakLock metodu
Parametrenin ne işe yaradığı ile ilgili bir soru burada.
Örnek
Şöyle yaparız.
Striped<Lock> keyLocks = Striped.lazyWeakLock(10)
lock metodu
Şöyle yaparız.
Striped<Lock>       locks = Striped.lock(1023);

Hiç yorum yok:

Yorum Gönder