The following program (ReaderOrWriter class) that implements the readers-writers problem with semaphores.

The main program (not shown) creates many reader and writer threads, starts each of them, and then waits forever for them to terminate by calling each threadís join() method.

Code:
import java.util.concurrent.*;

public class ReaderOrWriter implements Runnable
{
    private static int[] sharedBuffer = new int [1000];

    private static Semaphore s = new Semaphore(1000);
    private static Semaphore okToRead = new Semaphore(2);
    private static Semaphore okToWrite = new Semaphore(2);
    private static int activeWriters = 0;
    private static int waitingWriters = 0;
    private static int activeReaders = 0;
    private static int waitingReaders = 0;

    private static final int READER = 0;
    private static final int WRITER = 1;

    private int whichAml;

    ReaderOrWriter (int ReaderOrWriter )
    {
        whichAml = ReaderOrWriter ;
    }

    public void run()
    {
        while(true)
            if(whichAml == READER)
                reader();
            else
                writer();

    }

    private void reader() throws InterruptedException
    {
        s.acquire();
        if((activeWriters + waitingWriters) == 0)
        {
            okToRead.release();
            activeReaders++;
        }
        else
        {
            waitingReaders++;
        }
        s.release();
        okToRead.acquire();

        //perform read of sharedBuffer
        s.acquire();
        activeReaders--;
        if((activeReaders == 0)&&(waitingWriters > 0))
        {
            okToWrite.release();
            activeWriters++;
            waitingWriters--;
        }
        s.release();
    }

    private void writer()
    {
        //code not show
    }
}

What values should each of the semaphores be initialized to?
s: ??
okToRead: ??
okToWriteL ??

How can I tell what is the semaphore value just by looking at the codes?

How can I tell what is the semaphore value just by looking at the codes?

Assume a reader is currently using the buffer. If there are both waiting writers and waiting readers, will a writer or a reader get access to the buffer first?

Is it possible for readers to starve with this code?

Can the semaphore okToRead have a value greater than 1?

Can the semaphore okToWrite have a value greater than 1?