Concorrenza Java - Interfaccia ReadWriteLock
Un'interfaccia java.util.concurrent.locks.ReadWriteLock consente la lettura di più thread contemporaneamente, ma solo un thread può scrivere alla volta.
Read Lock - Se nessun thread ha bloccato ReadWriteLock per la scrittura, più thread possono accedere al blocco di lettura.
Write Lock - Se nessun thread sta leggendo o scrivendo, un thread può accedere al blocco di scrittura.
Metodi di blocco
Di seguito è riportato l'elenco dei metodi importanti disponibili nella classe Lock.
| Sr.No. | Metodo e descrizione | 
|---|---|
| 1 | public Lock readLock() Restituisce il lucchetto utilizzato per la lettura.  |  
| 2 | public Lock writeLock() Restituisce il blocco utilizzato per la scrittura.  | 
Esempio
Il seguente programma TestThread mostra questi metodi dell'interfaccia ReadWriteLock. Qui abbiamo usato readlock () per acquisire il read-lock e writeLock () per acquisire il write-lock.import java.util.concurrent.locks.ReentrantReadWriteLock;
public class TestThread {
   private static final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(true);
   private static String message = "a";
   public static void main(String[] args) throws InterruptedException {
      Thread t1 = new Thread(new WriterA());
      t1.setName("Writer A");
      
      Thread t2 = new Thread(new WriterB());
      t2.setName("Writer B");
      
      Thread t3 = new Thread(new Reader());
      t3.setName("Reader");
      t1.start();
      t2.start();
      t3.start();
      t1.join();
      t2.join();
      t3.join();
   }
   static class Reader implements Runnable {
      public void run() {
         
         if(lock.isWriteLocked()) {
            System.out.println("Write Lock Present.");
         }
         lock.readLock().lock();
         try {
            Long duration = (long) (Math.random() * 10000);
            System.out.println(Thread.currentThread().getName() 
               + "  Time Taken " + (duration / 1000) + " seconds.");
            Thread.sleep(duration);
         } catch (InterruptedException e) {
            e.printStackTrace();
         } finally {
            System.out.println(Thread.currentThread().getName() +": "+ message );
            lock.readLock().unlock();
         }
      }
   }
   static class WriterA implements Runnable {
      public void run() {
         lock.writeLock().lock();
         
         try {
            Long duration = (long) (Math.random() * 10000);
            System.out.println(Thread.currentThread().getName() 
               + "  Time Taken " + (duration / 1000) + " seconds.");
            Thread.sleep(duration);
         } catch (InterruptedException e) {
            e.printStackTrace();
         } finally {
            message = message.concat("a");
            lock.writeLock().unlock();
         }
      }
   }
   static class WriterB implements Runnable {
      public void run() {
         lock.writeLock().lock();
         
         try {
            Long duration = (long) (Math.random() * 10000);
            System.out.println(Thread.currentThread().getName() 
               + "  Time Taken " + (duration / 1000) + " seconds.");
            Thread.sleep(duration);
         } catch (InterruptedException e) {
            e.printStackTrace();
         } finally {
            message = message.concat("b");
            lock.writeLock().unlock();
         }
      }
   }
}
Questo produrrà il seguente risultato.
Produzione
Writer A  Time Taken 6 seconds.
Write Lock Present.
Writer B  Time Taken 2 seconds.
Reader  Time Taken 0 seconds.
Reader: aab                    