⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lockingmanager.java

📁 一个java rmi实现的分布式银行管理系统
💻 JAVA
字号:
package BS.server.xmllayer;

import java.util.ArrayList;

/**
 * @author 束罡
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */

public class LockingManager extends Thread
{
	public static ArrayList al = new ArrayList();
	
	public synchronized boolean synchronizedRequestLocking(int id, char lockType)
	{
		while (true)
		{
			if (requestLocking(id, lockType))
			{
				return true;
			}
			else
			{
				try
				{
					wait();
				}
				catch(InterruptedException e)
				{
					return false;
				}
			}
		}
	}
	
	public synchronized boolean synchronizedReleaseLocking (int id, char lockType)
	{
		while (true)
		{
			try
			{
				if (releaseLocking(id, lockType))
				{
					notifyAll();
					return true;
				}
				else
				{
					sleep(1);
				}
			}
			catch(InterruptedException e)
			{
				return false;
			}
		}
	}
	
	
	public static synchronized boolean requestLocking(int id, char lockType)
	{
		//遍历临界资源锁表al,并把对于临界资源id进行加锁的X_lock和S_lock找出来
		Lock ol = null;
		int xlock = 0;
		int slock = 0;
		for (int i=0; i<al.size(); i++)
		{
			ol = (Lock)al.get(i);
			if (ol.getId()==id)
			{
				if (ol instanceof X_lock)
				{
					xlock++;
					break;
				}
				else if (ol instanceof S_lock)
				{
					slock++;
				}
			}
		}
		
		
		//申请x,s锁的逻辑
		if (xlock > 0)
		{
			//如果临界资源已经被加了x锁,则不允许在加任何锁了
			return false;
		}
		else if (slock >= 0)
		{
			//如果临界资源已经没有被加x锁
			
			if (slock > 0 && (lockType == 'x'||lockType == 'X') )
			{
				//如果临界资源已经被加s锁,则不允许加x锁
				return false;
			}
			else if (slock > 0 && (lockType == 's'||lockType == 'S') )
			{
				//如果临界资源已经被加s锁,且还要加s锁,则允许加s锁
				S_lock sl = new S_lock(id);
				al.add(sl);
				return true;
			}
			else if (slock == 0)
			{
				//如果临界资源没有被加过任何锁,则允许加任何锁
				if (lockType == 'x'||lockType == 'X')
				{
					X_lock xl = new X_lock(id);
					al.add(xl);
					return true;
				}
				else if (lockType == 's'||lockType == 'S')
				{
					S_lock sl = new S_lock(id);
					al.add(sl);
					return true;
				}
			}
		}
		return false;
	}
	
	
	
	
	public static synchronized boolean releaseLocking (int id, char lockType)
	{
		Lock ol = null;
		for (int i=0; i<al.size(); i++)
		{
			ol = (Lock)al.get(i);
			if (ol.getId()==id)
			{
				if (ol instanceof X_lock)
				{
					if (lockType == 'X' || lockType == 'x')
					{
						al.remove(i);
						return true;
					}
				}
				if (ol instanceof S_lock)
				{
					if (lockType == 'S' || lockType == 's')
					{
						al.remove(i);
						return true;
					}
				}
			}
		}
		return false;
	}
	
	

	
	public static void main(String [] args)
	{
		LockingManager.prt((new LockingManager()).synchronizedRequestLocking(101,'x'));
		LockingManager.prt((new LockingManager()).synchronizedReleaseLocking(101,'x'));
		LockingManager.prt((new LockingManager()).synchronizedRequestLocking(101,'x'));
	}
	
	
	public static synchronized void prt (boolean flag)
	{
		if (flag==true)
		{
			System.out.println("true");
		}
		else
		{
			System.out.println("false");
		}
	}
}





class Lock
{
	protected int id = -1;
	public Lock (int id)
	{
		this.id = id;
	}
	public int getId()
	{
		return this.id;
	}
}

class X_lock extends Lock
{
	public X_lock(int id)
	{
		super(id);
	}
}

class S_lock extends Lock
{
	public S_lock(int id)
	{
		super(id);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -