Skip to main content
deleted 12 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I am trying to understand the workings of reader writer locks. For the sake of learning, I implemented reader writer synchronization by my own. I implemented SynchronizedArrayListSynchronizedArrayList with reader writer synchronization. 

I set out with following objective:

  1. There can be many readers.
  2. There can only be just one writer.
  3. Fairness among writers. In other words, writers should be served in FIFO manner.

I request reviewers to provide feedback on following:

  1. Correctness of code by considering the above set objectives.
  2. Efficiency of code.
  3. Any other improvements.

Code:

 
import java.util.ArrayList;

public class SynchronizedArrayList {
    private ArrayList<Object> list = new ArrayList<Object>();
    private int readerCount = 0;
    
    public void add(int index, Object obj)
    {
        synchronized(this) {
            while (readerCount != 0)
                waitForSignal();
        
            addInternal(index, obj);
        }
    }

    public Object get(int index) {
        synchronized(this) {
            readerCount++;
        }
        
        Object toReturn = getInternal(index);
        
        synchronized(this) {
            readerCount--;
            if (readerCount == 0) {
                this.notify();
            }
        }
        
        return toReturn;
        
    }

    private Object getInternal(int index) {
        return list.get(index);
    }
    
    private void addInternal(int index, Object obj) {
        list.add(index, obj);
    }
    
    private void waitForSignal() {
        //not yet implemented
    }
}

Thanks.

I am trying to understand the workings of reader writer locks. For the sake of learning, I implemented reader writer synchronization by my own. I implemented SynchronizedArrayList with reader writer synchronization. I set out with following objective:

  1. There can be many readers.
  2. There can only be just one writer.
  3. Fairness among writers. In other words, writers should be served in FIFO manner.

I request reviewers to provide feedback on following:

  1. Correctness of code by considering the above set objectives.
  2. Efficiency of code.
  3. Any other improvements.

Code:

import java.util.ArrayList;

public class SynchronizedArrayList {
    private ArrayList<Object> list = new ArrayList<Object>();
    private int readerCount = 0;
    
    public void add(int index, Object obj)
    {
        synchronized(this) {
            while (readerCount != 0)
                waitForSignal();
        
            addInternal(index, obj);
        }
    }

    public Object get(int index) {
        synchronized(this) {
            readerCount++;
        }
        
        Object toReturn = getInternal(index);
        
        synchronized(this) {
            readerCount--;
            if (readerCount == 0) {
                this.notify();
            }
        }
        
        return toReturn;
        
    }

    private Object getInternal(int index) {
        return list.get(index);
    }
    
    private void addInternal(int index, Object obj) {
        list.add(index, obj);
    }
    
    private void waitForSignal() {
        //not yet implemented
    }
}

Thanks.

I am trying to understand the workings of reader writer locks. For the sake of learning, I implemented reader writer synchronization by my own. I implemented SynchronizedArrayList with reader writer synchronization. 

I set out with following objective:

  1. There can be many readers.
  2. There can only be just one writer.
  3. Fairness among writers. In other words, writers should be served in FIFO manner.

I request reviewers to provide feedback on following:

  1. Correctness of code by considering the above set objectives.
  2. Efficiency of code.
  3. Any other improvements.
 
import java.util.ArrayList;

public class SynchronizedArrayList {
    private ArrayList<Object> list = new ArrayList<Object>();
    private int readerCount = 0;
    
    public void add(int index, Object obj)
    {
        synchronized(this) {
            while (readerCount != 0)
                waitForSignal();
        
            addInternal(index, obj);
        }
    }

    public Object get(int index) {
        synchronized(this) {
            readerCount++;
        }
        
        Object toReturn = getInternal(index);
        
        synchronized(this) {
            readerCount--;
            if (readerCount == 0) {
                this.notify();
            }
        }
        
        return toReturn;
        
    }

    private Object getInternal(int index) {
        return list.get(index);
    }
    
    private void addInternal(int index, Object obj) {
        list.add(index, obj);
    }
    
    private void waitForSignal() {
        //not yet implemented
    }
}
Source Link

SynchronizedArrayList implemented using reader writer synchronization

I am trying to understand the workings of reader writer locks. For the sake of learning, I implemented reader writer synchronization by my own. I implemented SynchronizedArrayList with reader writer synchronization. I set out with following objective:

  1. There can be many readers.
  2. There can only be just one writer.
  3. Fairness among writers. In other words, writers should be served in FIFO manner.

I request reviewers to provide feedback on following:

  1. Correctness of code by considering the above set objectives.
  2. Efficiency of code.
  3. Any other improvements.

Code:

import java.util.ArrayList;

public class SynchronizedArrayList {
    private ArrayList<Object> list = new ArrayList<Object>();
    private int readerCount = 0;
    
    public void add(int index, Object obj)
    {
        synchronized(this) {
            while (readerCount != 0)
                waitForSignal();
        
            addInternal(index, obj);
        }
    }

    public Object get(int index) {
        synchronized(this) {
            readerCount++;
        }
        
        Object toReturn = getInternal(index);
        
        synchronized(this) {
            readerCount--;
            if (readerCount == 0) {
                this.notify();
            }
        }
        
        return toReturn;
        
    }

    private Object getInternal(int index) {
        return list.get(index);
    }
    
    private void addInternal(int index, Object obj) {
        list.add(index, obj);
    }
    
    private void waitForSignal() {
        //not yet implemented
    }
}

Thanks.