15 Nisan 2016 Cuma

ReadWriteLock Arayüzü - Read ve Write Farkı Gözetir ve Reentrant'tır

Giriş
Şu satırı dahil ederiz.
import java.util.concurrent.locks.ReadWriteLock;
Açıklaması şöyle. Bu arayüzü gerçekleştiren tek sınıf ReentrantReadWriteLock.
A ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.
Kullanım
Örnek
Şöyle yaparız
public class Foo {
  ReadWriteLock lock = new ReentrantReadWriteLock();
  Lock readLock = lock.readLock();
  Lock writeLock = lock.writeLock();

  public void write (...) {
    writeLock.lock();
    try {
      ...
    } finally {
      writeLock.unlock();
    }
  }

  public void read (...) {
    readLock.lock();
    try {
      ...
    } finally {
      readLock.unlock();
    }
  }
}

Hiç yorum yok:

Yorum Gönder