使用while的spin lock和直接用boolean + if完成lock 的例子

CriticalResourceUsingSynchronized.java

// This solves the readers/writers problem only using synchronized

public class CriticalResourceUsingSynchronized
{
private Integer m_readLock = new Integer(0);
private int m_reading = 0;

public int read()
{
int readData = 0;

// lock readers
synchronized( m_readLock )
{
// if we are the first reader lock writers

// this is what we wanted to do from the pseudo code:
// if( m_reading == 0 )
// lock writers
//
// But there is no way to do this with synchronized so
// the writer has to do a spin lock on m_reading

// increment semaphore
m_reading++;

} // unlock readers

// do read (may take significant time...)

// lock readers
synchronized( m_readLock )
{
// decrement semaphore
m_reading--;

// from the pseudo code we wanted to do this:
// if( m_reading == 0 )
// unlock writers
//
// but synchonized doesn't cut it here so the writer is
// polling m_reading (i.e. spin lock) to see when it goes to 0

}// unlock readers

// return the read data
return readData;
}

public void write( int x )
{
// we want to
//
// lock writers
// write
// unlock writers
//
// but since we are only using synchronized we have to spin lock;
// because synchronized is not a lock; it is this weird linked
// critical section thing.

// spin lock waiting for m_readers to go to 0
boolean succeeded = false;
while( !succeeded )
{
synchronized( m_readLock )
{
if( m_reading == 0 )
{
// we did it! m_readers is 0, and no readers can get in
// because we have locked m_readLock

// do write (may take significant time...)

// we can break out of this crazy spin lock!
succeeded = true;
}
}

if( succeeded == false )
{
// we failed; there are still readers so let them compute
// so yeild the rest of our time slice so we don't check
// again before any other thread has a chance to do anything
Thread.currentThread().yield();
}
}
}
}
<!-- /////////////////////// End Segment /////////////////////// -->

Lock.java

// Lock.java
//
// This class implements a boolean lock object in java
//

class Lock extends Object
{
private boolean m_bLocked = false;

public synchronized void lock()
{
// if some other thread locked this object then we need to wait
// until they release the lock
if( m_bLocked )
{
do
{
try
{
// this releases the synchronized that we are in
// then waits for a notify to be called in this object
// then does a synchronized again before continuing
wait();
}
catch( InterruptedException e )
{
e.printStackTrace();
}
catch( Exception e )
{
e.printStackTrace();
}
} while( m_bLocked ); // we can't leave until we got the lock, which
// we may not have got if an exception occured
}

m_bLocked = true;
}

public synchronized boolean lock( long milliSeconds )
{
if( m_bLocked )
{
try
{
wait( milliSeconds );
}
catch( InterruptedException e )
{
e.printStackTrace();
}

if( m_bLocked )
{
return false;
}
}

m_bLocked = true;
return true;
}

public synchronized boolean lock( long milliSeconds, int nanoSeconds )
{
if( m_bLocked )
{
try
{
wait( milliSeconds, nanoSeconds );
}
catch( InterruptedException e )
{
e.printStackTrace();
}

if( m_bLocked )
{
return false;
}
}

m_bLocked = true;
return true;
}

public synchronized void releaseLock()
{
if( m_bLocked )
{
m_bLocked = false;
notify();
}
}

public synchronized boolean isLocked()
{
return m_bLocked;
}
}
<!-- /////////////////////// End Segment /////////////////////// -->

CriticalResourceUsingSynchronizedAndLocks.java

// This solves the readers/writers problem only using synchronized and the Lock class
import Lock;

public class CriticalResourceUsingSynchronizedAndLocks
{
private Integer m_readLock = new Integer(0);
private int m_reading = 0;
private Lock m_writeLock = new Lock();

public int read()
{
int readData = 0;

// lock readers
synchronized( m_readLock )
{
// if we are the first reader lock writers

if( m_reading == 0 )
// lock the writers
m_writeLock.lock();

// increment semephore
m_reading++;

} // unlock readers

// do read (may take significant time...)

// lock readers
synchronized( m_readLock )
{
// decrement semaphore
m_reading--;

if( m_reading == 0 )
// release the writers
m_writeLock.releaseLock();

}// unlock readers

// return read value
return readData;
}

public void write( int x )
{
// lock writers
m_writeLock.lock();

// do writing

// release writers
m_writeLock.releaseLock();
}
}
<!-- /////////////////////// End Segment /////////////////////// -->

CriticalResourceUsingLocks.java

// This solves the readers/writers problem only the Lock class
import Lock;

public class CriticalResourceUsingLocks
{
private Lock m_readLock = new Lock();
private int m_reading = 0;
private Lock m_writeLock = new Lock();

public int read()
{
int readData = 0;

// lock readers
m_readLock.lock();

// if we are the first reader lock writers
if( m_reading == 0 )
// lock the writers
m_writeLock.lock();

// increment semephore
m_reading++;

// unlock readers
m_readLock.releaseLock();

// do read (may take significant time...)

// lock readers
m_readLock.lock();

// decrement semaphore
m_reading--;

if( m_reading == 0 )
// release the writers
m_writeLock.releaseLock();

// unlock readers
m_readLock.releaseLock();

// return read data
return readData;
}

public void write( int x )
{
// lock writers
m_writeLock.lock();

// do writing

// release writers
m_writeLock.releaseLock();
}
}
<!-- /////////////////////// End Segment /////////////////////// -->

你可能感兴趣的:(thread,REST,Go)