21 Ağustos 2019 Çarşamba

ImmutableMap.Builder Sınıfı

build metodu
Örnek
Şöyle yaparız.
ImmutableMap.Builder<String,String> myMap = ImmutableMap.<String, String>builder()
    .put("key1", "value1") 
    .put("key2", "value2") 
    .put("key3", "value3") 
    .put("key4", "value4") 
    .put("key5", "value5") 
    .put("key6", "value6") 
    .put("key7", "value7") 
    .put("key8", "value8") 
    .put("key9", "value9")
    .build();
put metodu
Örnek
Şöyle yaparız.
final Map<String, Integer> m = ImmutableMap.<String, Integer>builder().
      put("a", 1).
      put("b", 2).
      build();
putAll metodu
Şöyle yaparız.
Map<String, String> srcMap = Map.of("A", "a", "B", "b", "C", "c");

ImmutableMap<String, String> map = ImmutableMap.<String, String> builder()
  .putAll(srcMap)
  .put("D", "d")
  .build();

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);