javamultithreadinglinearization

Determination of Linearization order


How exactly is linearization order determined. How can it be said that Linearization order of the following code is the order in which released by wait(). How can it be checked whether the code is linearizable?

class Buffer
{
    int in = 0;
    int out = 0;
    int numElems = 0;

    synchronized void put(E elem) throws InterruptedException
    {
        while (!numElems < N)
        {
            wait();
        }
        buff[in] = elem;
        in = (in + 1) % N;
        notifyAll();
    }

    synchronized E take() throws InterruptedException
    {
        while (!numElems > 0)
        {
            wait();
        }
        E temp = buff[out];
        out = (out + 1) % N;
        return temp;
        notifyAll();
    }
}

Solution

  • There's only one lock here (the lock on one specific Buffer object), so the linearization order is just the order in which that lock is acquired (or released - it is the same order). The lock is always released on entry to wait and acquired on exit from wait, which is why you've heard about the wait release order in this context.

    I'm not sure what you mean by "check that it is linearizable". If by that you mean that any parallel execution is equivalent to some serial ordering, then it is pretty obvious here (although hard in general) because all accesses to memory are under a single lock, so there is effectively no parallel execution.