Java 等待锁定操作时线程永远被阻塞
我正在编写一个用于两阶段锁定的java实现。所以,我使用可重入锁(ReadWrite锁)。问题是,当线程执行lock.readLock.lock()或lock.writeLock().lock()时,即使使用lock.readLock().unlock()或lock.writeLock().unlock()解锁了锁,该锁也会永远被锁定。所以,看起来解锁并没有唤醒服务员!!! 以下是导致问题的代码:Java 等待锁定操作时线程永远被阻塞,java,multithreading,locking,reentrantreadwritelock,Java,Multithreading,Locking,Reentrantreadwritelock,我正在编写一个用于两阶段锁定的java实现。所以,我使用可重入锁(ReadWrite锁)。问题是,当线程执行lock.readLock.lock()或lock.writeLock().lock()时,即使使用lock.readLock().unlock()或lock.writeLock().unlock()解锁了锁,该锁也会永远被锁定。所以,看起来解锁并没有唤醒服务员!!! 以下是导致问题的代码: class LockTable { // /************************
class LockTable
{
// /*******************************************************************************
// * This class is used to represent an individual lock.
// * @param tid the id of the transaction holding the lock
// * @param shared whether the lock is shared (true) or exclusive (false)
// */
// void Lock (int tid, boolean shared)
// {
// Semaphore sem = new Semaphore (0);
// } // Lock class
/** Associative map of locks held by transactions of the form (key = oid, value = lock)
*/
private HashMap<Integer,MyLock> locks;
public LockTable(){
locks= new HashMap<Integer,MyLock>();
}
/*******************************************************************************
* Acquire a shared/read lock on data object oid.
* @param tid the transaction id
* @param oid the data object id
*/
void rl (int tid, int oid) throws InterruptedException
{
MyLock lock=null;
boolean wait = false;
synchronized(this) {
try {
lock = locks.get(oid); // find the lock
if((lock != null) && (lock.lock.isWriteLocked())){
wait = true;
// System.out.println(locks.get(oid).shared);
}
if(lock == null){
lock = new MyLock(tid, true);
lock.lock.readLock().lock();
lock.readers.add(tid);
locks.put(oid, lock);
}
} catch(Exception e) {
System.out.println(e.getStackTrace()); // lock not found, so oid is not locked;
} // try
}//synch
if (wait){
System.out.println("Transaction " + tid + " is waiting..");
Main.g.addEdge(tid, lock.tid);
if(Main.g.hasCycle())
restart(tid);
//to exclude the restarted thread
if(!Main.trans[tid].terminate){
lock.lock.readLock().lock();
Main.g.removeEdge(tid, lock.tid);
synchronized(this){
lock.readers.add(tid);
}//synchronized
}//if isInturrupted
else
return;
}
else
synchronized(this) {
lock.lock.readLock().lock();
lock.readers.add(tid);
} // synchronized
} // rl
/*******************************************************************************
* Acquire an exclusive/write lock on data object oid.
* @param tid the transaction id
* @param oid the data object id
*/
void wl (int tid, int oid) throws InterruptedException
{
//type to determine the last lock type in order
//to be able to remove the edges from waitfor graph
int type = 0;
MyLock lock = null;
boolean wait = false;
synchronized(this) {
try {
lock = locks.get(oid); // find the lock
if(lock != null && (lock.lock.isWriteLocked() || lock.readers.size() > 0))
{
wait = true;
}
if(lock == null){
lock = new MyLock(tid);
lock.lock.writeLock().lock();
locks.put(oid,lock);
}
} catch(Exception e) {
System.out.println(e.getStackTrace()); // lock not found, so oid is not locked;
} // try
}
if (wait){
System.out.println("Transaction " + tid + " is waiting..");
if(lock.lock.isWriteLocked())
Main.g.addEdge(tid, lock.tid);
else{
type = 1;
for(int reader : lock.readers)
Main.g.addEdge(tid, reader);
}//else
if(Main.g.hasCycle())
{
restart(tid);
}//if
if(!Main.trans[tid].terminate){
System.out.println("I'm waiting here in wl");
lock.lock.writeLock().lock();
System.out.println("Wakeup..");
if(type == 0)
Main.g.removeEdge(tid, lock.tid);
else
for(int reader : lock.readers)
Main.g.removeEdge(tid, reader);
lock.tid = tid;
}
else
return;
}// if(wait) ==> for the lock to be released
else
lock.lock.writeLock().lock();
} // wl
void restart(int tid){
synchronized(this) {
MyLock lock;
List<Integer> toRemove = new ArrayList();
for(int i : locks.keySet()){
lock = locks.get(i);
//lock.sem.release();
if(lock.lock.isWriteLockedByCurrentThread()){
System.out.println("Transaction"+tid+" unlock object "+ i +" in order to restart");
lock.lock.writeLock().unlock();
System.out.println("number of write holders: " + lock.lock.writeLock().getHoldCount());
System.out.println("number of read holders: " + lock.lock.getReadHoldCount());
System.out.println("number of waiters: " + lock.lock.getQueueLength());
toRemove.add(i);
}
if(!lock.lock.isWriteLocked())
if(lock.readers.contains(tid)){
// lock.numberOfReaders --;
System.out.println("Transaction"+tid+" unlock object "+ i +" in order to restart");
lock.readers.remove(lock.readers.indexOf(tid));
lock.lock.readLock().unlock();
System.out.println("number of write holders: " + lock.lock.getWriteHoldCount());
System.out.println("number of read holders: " + lock.lock.getReadHoldCount());
System.out.println("number of waiters: " + lock.lock.getQueueLength());
toRemove.add(i);
}//if
}//for
for(int i = 0; i < toRemove.size() ; i ++)
locks.remove(toRemove.get(i));
Main.g.removeEdges(tid);
// Thread.currentThread().interrupt();
Main.trans[tid].terminate = true;
System.out.println("Transaction" + tid + " restarted");
}//sync
}
/*******************************************************************************
* Unlock/release the lock on data object oid.
* @param tid the transaction id
* @param oid the data object id
*/
void ul (int tid, int oid)
{
MyLock lock = null;
boolean error = false;
synchronized(this) {
try {
lock = locks.get(oid); // find the lock
if( lock == null)
System.out.println("println: lock not found");
} catch(Exception e) {
System.out.println("lock not found"); // lock not found
} // try
}//sync
if((lock != null) && (lock.lock.isWriteLockedByCurrentThread())){
System.out.println("tid: " + tid + " unlock object: " + oid);
lock.lock.writeLock().unlock();
System.out.println("done with unlock");
System.out.println("number of write holders: " + lock.lock.writeLock().getHoldCount());
System.out.println("number of read holders: " + lock.lock.getReadHoldCount());
System.out.println("number of waiters: " + lock.lock.getQueueLength());
}// if lock != null
else
if((lock != null) && (lock.readers.size()>0)){
if(lock.readers.contains(tid)){
lock.readers.remove(lock.readers.indexOf(tid));
lock.lock.readLock().unlock();
System.out.println("Transaction"+tid+" unlocked shared lock on object "+oid);
//System.out.println("number of write holders: " + lock.lock.readLock().);
System.out.println("number of read holders: " + lock.lock.getReadHoldCount());
System.out.println("number of waiters: " + lock.lock.getQueueLength());
}//if lock.readers
}//if
if (error)
System.out.println ("Error: ul: no lock for oid = " + oid + " found/owned");
} // ul
类锁表
{
// /*******************************************************************************
//*此类用于表示单个锁。
//*@param tid持有锁的事务的id
//*@param shared锁是共享的(true)还是独占的(false)
// */
//无效锁(int-tid,布尔共享)
// {
//信号量sem=新信号量(0);
//}//锁类
/**表单事务持有的锁的关联映射(key=oid,value=lock)
*/
私有HashMap锁;
公共锁台(){
locks=newhashmap();
}
/*******************************************************************************
*获取数据对象oid上的共享/读取锁定。
*@param tid事务id
*@param oid数据对象id
*/
void rl(inttid,intoid)抛出InterruptedException
{
MyLock lock=null;
布尔等待=假;
已同步(此){
试一试{
lock=locks.get(oid);//找到锁
if((lock!=null)&&(lock.lock.isWriteLocked()){
等待=真;
//System.out.println(locks.get(oid.shared));
}
if(lock==null){
锁定=新的MyLock(tid,true);
lock.lock.readLock().lock();
lock.readers.add(tid);
锁。放(oid,锁);
}
}捕获(例外e){
System.out.println(e.getStackTrace());//未找到锁,因此oid未锁定;
}//试试看
}//同期
如果(等待){
System.out.println(“事务”+tid+“正在等待…”);
主.g.补遗(tid,锁.tid);
if(Main.g.hasCycle())
重新启动(tid);
//要排除重新启动的线程
如果(!Main.trans[tid]。终止){
lock.lock.readLock().lock();
主g.移除(tid,锁定tid);
已同步(此){
lock.readers.add(tid);
}//同步的
}//如果没有中断
其他的
返回;
}
其他的
已同步(此){
lock.lock.readLock().lock();
lock.readers.add(tid);
}//已同步
}//rl
/*******************************************************************************
*获取数据对象oid上的独占/写入锁定。
*@param tid事务id
*@param oid数据对象id
*/
void wl(inttid,intoid)抛出InterruptedException
{
//键入以确定顺序中的最后一种锁类型
//能够从waitfor图中删除边
int类型=0;
MyLock lock=null;
布尔等待=假;
已同步(此){
试一试{
lock=locks.get(oid);//找到锁
if(lock!=null&(lock.lock.isWriteLocked()| | lock.readers.size()>0))
{
等待=真;
}
if(lock==null){
锁定=新的MyLock(tid);
lock.lock.writeLock().lock();
锁。放(oid,锁);
}
}捕获(例外e){
System.out.println(e.getStackTrace());//未找到锁,因此oid未锁定;
}//试试看
}
如果(等待){
System.out.println(“事务”+tid+“正在等待…”);
if(lock.lock.isWriteLocked())
主.g.补遗(tid,锁.tid);
否则{
类型=1;
for(int reader:lock.readers)
主附录g(tid,读卡器);
}//否则
if(Main.g.hasCycle())
{
重新启动(tid);
}//如果
如果(!Main.trans[tid]。终止){
System.out.println(“我在wl里等着”);
lock.lock.writeLock().lock();
System.out.println(“唤醒…”);
如果(类型==0)
主g.移除(tid,锁定tid);
其他的
for(int reader:lock.readers)
Main.g.removedge(tid,读卡器);
lock.tid=tid;
}
其他的
返回;
}//如果(等待)==>等待释放锁
其他的
lock.lock.writeLock().lock();
}//wl
无效重新启动(int tid){
已同步(此){
麦洛克锁;
List toRemove=new ArrayList();
for(int i:locks.keySet()){
lock=locks.get(i);
//lock.sem.release();
if(lock.lock.isWriteLockedByCurrentThread()){
System.out.println(“事务”+tid+“解锁对象”+i+“以便重新启动”);
lock.lock.writeLock().unlock();
System.out.println(“写入持有者的数量:+lock.lock.writeLock().getHoldCount());
System.out.println(“读取持有者的数量:+lock.lock.getReadHoldCount());
System.out.println(“等待者数量:+lock.lock.getQueueLength());
删除。添加(i);
}
如果
Method A:
...snip...
synchronized(this) {
...snip...
lock.lock.readLock().lock();
...snip...
}
Method B:
...snip...
synchronized(this) {
...snip...
lock.lock.readLock().unlock();
...snip...
}