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

本文探讨了使用synchronized关键字和自定义锁解决读者-写者问题的不同方法。通过三种不同的类实现:仅使用synchronized,结合synchronized和自定义Lock类,以及完全依赖自定义Lock类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 /// -->

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值